Reading integers in from a file
Hi, I think I posted this in the wrong forum before... anyway, my question was:
Hi, I am going to go for the 'reading in from a text file' action as I haven't done this before. So I have been looking at all the examples and am trying the one out below :) My question is that though the structure seems pretty straightforward, what is the action of the 'token' and why doesn't java recognise it as it is used in many different examples that I have seen?
public static int[] getIntegersFromFile(String fileName) throws IOException {
StringWriter writer = new StringWriter();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
List<Integer> list = new ArrayList<Integer>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
writer.write(line + " ");
}
StringTokenizer tokens = new StringTokenizer(writertoString());
while (tokens.hasMoreTokens()) {
String str = tokens.nextToken();
try {
list.add(new Integer(str));
} catch (NumberFormatException e) {
System.out.println("Error '" + str + "' is not an integer.");
}
}
int[] array = new int[list.size()];
for( int i = 0; i < array.length; i++){
array = list.get(i);
}
return array;
}

