gamepro said:
I need to save a line of .TXT as an int
The file has 10 lines each with an integer and I need to be able to get each integer into a variable.
Thanks in advance
Warning: this code is horrible. If this is for a homework assignment and your teacher knows anything about Java, you will get
beaten for not making anything based on this a lot more pretty.
Code:
import java.io.*;
public class FileReader {
public static void main(String[] args) throws Exception {
String filename = args[0];
Reader r = new FileReader(filename);
BufferedReader br = new BufferedReader(r);
String line = br.readLine();
while (line != null) {
System.out.println("Read: " + line);
line = br.readLine();
}
}
}
Hell, if you use any of that line for line, I'll track you down and beat you myself. But it should give you a start.
Edit: And it isn't tested, because I have no compiler with me, so it's bound to be wrong in a couple of ways.
Edit2: That's just for reading in the lines of text - if you don't know how to convert to an integer, check out the Javadoc for the Integer class.