Reading from a file, multiple lines
This isn't homework, I'm only in 8th grade my school doesn't have java courses before you ask)<--
Well I ran into a funny little problem, not quite sure how I should approach it;
Well I have a file looking like this:
1 0 3 2
2 3 4 5
And so on going down like 20 lines.
Here is my code:
It is meant to read the file, and to test it I had it print out the lines, but it only prints out the first line of the file.
I know that my method readLine(); reads the first line only, how would I get it to read the next line?
Code :
public class getNumbersFromFile
{
public static void main(String[] args) {
getNumbersFromFile file = new getNumbersFromFile();
file.getFileFromMethod();
}
public void getFileFromMethod(){
try {
BufferedReader FileReader = new BufferedReader(new FileReader("SudokuBoard.txt"));
String Text = FileReader.readLine();
String[] Split = Text.split(" ");
System.out.println(Text);
} catch (IOException e) {
System.out.println("Some sort of interruption.");
System.out.println(e);
}
}
}
Re: Reading from a file, multiple lines
if u want to display all lines of txt change this
Code :
String[] Split = Text.split(" ");
System.out.println(Text);
to this
Code :
String[] Split = Text.split(" ");
int count = 0
while ( Split[count] != null ) {
System.out.println(Split[count]);
count = count + 1
}
that should work i think
but the main problem from what i got was that u were displaying text, not the Split array
Re: Reading from a file, multiple lines
Quote:
Originally Posted by
wolfgar
if u want to display all lines of txt change this
Code :
String[] Split = Text.split(" ");
System.out.println(Text);
to this
Code :
String[] Split = Text.split(" ");
int count = 0
while ( Split[count] != null ) {
System.out.println(Split[count]);
count = count + 1
}
that should work i think
but the main problem from what i got was that u were displaying text, not the Split array
I did that;
What i get as output,then error:
0
0
0
0
6
0
0
0
9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at getNumbersFromFile.getFileFromMethod(getNumbersFro mFile.java:25)
at getNumbersFromFile.main(getNumbersFromFile.java:10 )
Re: Reading from a file, multiple lines
Here is a snippet of code to read a file, line by line.
Code :
public static void main(String[] args) throws Exception {
final BufferedReader bufferedReader = new BufferedReader(new FileReader("SudokuBoard.txt"));
if (bufferedReader != null) {
String line;
// Keep on looping while line is not null, readLine will return null when it reaches the end of the file and the loop will stop.
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
}
See also
http://www.javaprogrammingforums.com...line-file.html
// Json
Re: Reading from a file, multiple lines
Quote:
Originally Posted by
Json
Here is a snippet of code to read a file, line by line.
Code :
public static void main(String[] args) throws Exception {
final BufferedReader bufferedReader = new BufferedReader(new FileReader("SudokuBoard.txt"));
if (bufferedReader != null) {
String line;
// Keep on looping while line is not null, readLine will return null when it reaches the end of the file and the loop will stop.
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
}
See also
http://www.javaprogrammingforums.com...line-file.html
// Json
Jason I love you ;).
It works, and I'm reading the post you linked.
Re: Reading from a file, multiple lines
Glad I could help :)
// Json