0

Is there a way for java to be able to tell when a line in the text file ends?

I'm trying to put information from a text file into an int array

1 2 3 4 5 6 7 8 9

If the text file was the above numbers, I would want the code to be able to store 1 2 3 and then be able to tell it to stop because the next numbers are in a new line

I couldn't find any boolean in the scanner oracle doc that could help me with this

The amount of numbers in the first line of the text file can vary so I don't want it to stop after only reading 3 numbers, which is what my code currently has

2 Answers2

0

You can read line-by-line using BufferedReader as follows:

public class ReadLine {

public static void main(String[] args) throws IOException {
    File f = new File("");
    BufferedReader br = new BufferedReader(new FileReader(f));
    try {
        String line = br.readLine();
        while(line != null) {
            line = br.readLine();
            final Scanner s = new Scanner(line);
            while(s.hasNextInt()) {
                final int i = s.nextInt();
                // code using i here
            }
        }
    } finally {
        br.close();
    }
}

}

dimplex
  • 494
  • 5
  • 9
-1

It sounds like you want to read by line then with Scanner.nextLine() and Scanner.hasNextLine(). Of course, if you can use Apache Commons then FileUtils.readLines(File, String) makes it easy to read the file line-by-line. Once you have a line, you can use Scanner(String) which (per the Javadoc)

Constructs a new Scanner that produces values scanned from the specified string.

Something like,

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    Scanner strScanner = new Scanner(line);
    // ...
}

As @JonSkeet pointed out in the comments, you can also split the line on white-space like

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    String[] arr = line.split("\\s+");
    // ...
}

And as @RealSkeptic points out below might also use Files.readAllLines(Path). That might give you a full example like

// Get the user's home directory
String home = System.getProperty("user.home");
File f = new File(home, "input.txt");
try {
    // Get all of the lines into a List
    List<String> lines = Files.readAllLines(f.toPath());
    // Get the line count to create an array.
    int[][] arr = new int[lines.size()][];
    // populate the array.
    for (int i = 0; i < lines.size(); i++) {
        // split each line on white space
        String[] parts = lines.get(i).split("\\s+");
        arr[i] = new int[parts.length];
        for (int j = 0; j < parts.length; j++) {
            // parse each integer.
            arr[i][j] = Integer.parseInt(parts[j]);
        }
    }
    // Display the multidimensional array.
    System.out.println(Arrays.deepToString(arr));
} catch (IOException e) {
    // Display any error.
    e.printStackTrace();
}
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Note that Apache Commons is not needed - one cas use [`Files.readAllLines()`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29) – RealSkeptic Sep 06 '15 at 18:23