If you do not know the number of lines in the file in advance, I suggest you collect the numbers in two Lists, for example ArrayList<Integer>.
Something like this should do the trick:
List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
Scanner s = new Scanner(new FileReader("filename.txt"));
while (s.hasNext()) {
l1.add(s.nextInt());
l2.add(s.nextInt());
}
s.close();
System.out.println(l1);
System.out.println(l2);
If you really need numbers in two arrays int[], you can create arrays later (when the size is known).
source
share