Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting integer data from txt file in Java

I want to read a text file which has String and a few integers related to that string.

This is the class that I have to write my program in:

public List<Integer> Data(String name) throws IOException {
    return null;
}

I have to read the .txt file and find the name in that file, with its data. And save it in an ArrayList.

My question is how do I save it in the ArrayList<Integer> when I have Strings in the List.
This is what I think I would do:

Scanner s = new Scanner(new File(filename));
ArrayList<Integer> data = new ArrayList<Integer>();

while (s.hasNextLine()) {
    data.add(s.nextInt());
}
s.close();
like image 928
JavaBeginner Avatar asked Jun 07 '15 04:06

JavaBeginner


People also ask

Which method can be used to read an integer number from a text file?

Once the Scanner is open on the file, we can use the usual command nextInt() to read the next available integer from the file. The program will attempt to read all of the numbers in the text file and print them to the output pane.

How do I read an int from a file?

To read integers from a file, we use BufferedReader to read the file and the parseInt() method to get the integers from the data.


1 Answers

I would define the file as a field (in addition to the filename, and I suggest reading it from the user's home folder) file

private File file = new File(System.getProperty("user.home"), filename);

Then you can use the diamond operator <> when you define your List. You can use a try-with-resources to close your Scanner. You want to read by lines. And you can split your line. Then you test if your first column matches the name. If it does, iterate the other columns are parse them to int. Something like

public List<Integer> loadDataFor(String name) throws IOException {
    List<Integer> data = new ArrayList<>();
    try (Scanner s = new Scanner(file)) {
        while (s.hasNextLine()) {
            String[] row = s.nextLine().split("\\s+");
            if (row[0].equalsIgnoreCase(name)) {
                for (int i = 1; i < row.length; i++) {
                    data.add(Integer.parseInt(row[i]));
                }
            }
        }
    }
    return data;
}

It might be signifanctly more efficient to scan the file once and store the names and fields as a Map<String, List<Integer>> like

public static Map<String, List<Integer>> readFile(String filename) {
    Map<String, List<Integer>> map = new HashMap<>();
    File file = new File(System.getProperty("user.home"), filename);
    try (Scanner s = new Scanner(file)) {
        while (s.hasNextLine()) {
            String[] row = s.nextLine().split("\\s+");
            List<Integer> al = new ArrayList<>();
            for (int i = 1; i < row.length; i++) {
                al.add(Integer.parseInt(row[i]));
            }
            map.put(row[0], al);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}

Then store that as fileContents like

private Map<String, List<Integer>> fileContents = readFile(filename);

And then implement your loadDataFor(String) method with fileContents like

public List<Integer> loadDataFor(String name) throws IOException {
    return fileContents.get(name);
}

If your usage pattern reads the File for many names then the second is likely to be much faster.

like image 192
Elliott Frisch Avatar answered Oct 14 '22 22:10

Elliott Frisch