Reading in integers from a text file
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;
}
[1268 byte] By [
cakea] at [2007-11-26 19:53:14]

Hey, first of all, a piece of advice, try formatting your codes you make easier
to understand for the people who help you. You can check out the
formatting tips
Now, A think that could help you a lot, is the next link, seek out the
documentation for the StringTokenizer class
http://java.sun.com/j2se/1.5.0/docs/api/
Here your code formated and later my interpretation of your question.
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;
}
StringTokenizer separates the string contained by the space character, and
each piece of the separated string is stored into the token.
Further on, could you extend your question about "why doesn't Java
recognize what?" please, because I don't know if you have a problem
iterating or is just that it doesn't compile or what.
Expecting your answer, to complete mine, cya around.
-Best Regards.
Hi @lphazygma,
Sorry about the formatting I was just eager to get the post up. Ok, I am trying to read in from a file flushes.txt which is structured in the following manner 0,0,1934,0,0,1454,0,0...
I simply want to read in the txt and convert them to an integer array so that I can then do searches on it.
StringTokenizer tokens = new StringTokenizer(writertoString());
The line above keeps throwing the error writerToString cannot be resolved and that is because I actually don't know quite what it is doing. I am still reading about what each aspect does, and this is one of the examples that I found.
Anyway, thanks for the reply, Ron
cakea at 2007-7-9 22:44:49 >

cannot be resolved means the compiler can't find the method/variable stated. Did you define the method somewhere in the class? If it's in another class you need to call them with prefix like Class.method() if its static, or instance.method() otherwise.
Read Reply #5 in the thread where you initially posted this code: http://forum.java.sun.com/thread.jspa?threadID=5141967You didn't need to create a new thread [and worse yet, two new threads!] for this part of the question--you should have waited longer for an answer.
Hey cake
I agree with Icycool, the method writertoString() should be somewhere in your
code, or must be a part of a class you are not importing.
That is why the compiler says it cannot be resolved.
Now, StringTokenizer won't work since your file contains a comma separated
numbers, and not space separated numbers, this is because the default
constructor for StringTokenizer separates text tokens by spaces and not by
commas, then, you should use another constructor
StringTokenizer(String str, String delim)
And call it like this
StringTokenizer(your_text_variable, ",")
And for the for cycle, instead of adding an space, add a comma, like this:
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
writer.write(line + ",");
}
But finally, as the use of this class is discouraged, I will prompt you to store
the whole file into an String like:
String file = "";
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
file += line + ",";
}
String[] numbers = file.split(",");
int[] array = new int[numbers.length];
for(int i = 0; i < array.length; i++){
array = Integer.parseInt( numbers[i] );
}
I think that could help you.
Cya around.
Sorry to say, @lphazygma, but most of your advice isn't the best.
writertoString was simply a typo--missing a period before "toString".
As shown in the other thread, cake is now using a Scanner and creating the List (soon to be a Map, hopefully) directly, instead of reading data into a String and then parsing it. If he did want to use a String and a StringTokenizer, using spaces everywhere would have worked. But, that is the long way to do it. The String containing the whole file isn't needed--he just needs the numbers. So, reading and parsing at the same time will be much faster than reading the whole thing, creating a big String, and then breaking the String up again.