I have this text file and I need to extract it to a Java program to determine array sizes and its values I can read each line alone but I can't get the value for lines that contains many numbers. Plus, how to count element in the third line.( 0 2 )
4
5
0 2
0 1 0.6
0 2 0.2
0 3 0.5
1 3 0.8
2 3 0.3
this code that I'm using:
List<Integer> list = new ArrayList<Integer>();
File file = new File("D:\\ALGORTHIMS\\MASTER LEVEL\\dr. khaled\\assignment 1\\text.txt");
BufferedReader reader = null;
List<Double> ints = new ArrayList<Double>();
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
if (text.length() == 1) {
list.add(Integer.parseInt(text));
} else {
String[] strs = text.trim().split("\\s+");
for (int i = 0; i < strs.length; i++) {
ints.add(Double.parseDouble(strs[i]));
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
//print out the list
System.out.println(list);
System.out.println(ints);