While loop help needed
I have a text filewhich I am reading from but the text is on several lines.
I need to be able to read every line of text and display it and need to know how to use a while loop to get all the text lines.
Here is the code that reads the text file but it only reads the first line
FileReader file = new FileReader(fileName);
BufferedReader MyFile = new BufferedReader(file);
//add code to read line here
currentLine = MyFile.readLine();
System.out.println(currentLine);
I need to be able to read all the lines. Any help would be gratefullt appreciated
[611 byte] By [
AdRock] at [2007-11-11 10:06:53]

# 1 Re: While loop help needed
FileReader file = new FileReader(fileName);
BufferedReader MyFile = new BufferedReader(file);
String currentLine;
while((currentLine = MyFile.readLine()) != null) {
System.out.println(currentLine);
}
# 2 Re: While loop help needed
That seems to work which is what I tried earlier but i get this error message
Exception in thread "main" java.lang.NullPointerException......
The text document I'm trying to read has blank line spaces so I don't know if that is causing the problem (like how you see this post)
AdRock at 2007-11-11 22:32:54 >

# 3 Re: While loop help needed
Tried using Scanner?
masher at 2007-11-11 22:34:04 >

# 4 Re: While loop help needed
That seems to work which is what I tried earlier but i get this error message
Exception in thread "main" java.lang.NullPointerException......
The text document I'm trying to read has blank line spaces so I don't know if that is causing the problem (like how you see this post)
My guess is MyFile is null. Can't say much about it without more details.
# 5 Re: While loop help needed
Here is an example text document i might want to include
blah blah balh blah blah
blah blah balh blah blah blah blah balh
blah blah balh blah blah
blah blah balh blah blah blah blah balh blah bla
blah blah balh blah blah
I have noticed that it all works if i use these lines which isn't in a while loop
String textline = null;
textline = MyFile.readLine();
System.out.println(textline);
but as soon as i put it in a while loop it throws up the error.
Here is the code which works witout the while loop
FileReader file = new FileReader(fileName);
BufferedReader MyFile = new BufferedReader(file);
Document myDoc = new Document(fileName);
String textline = null;
textline = MyFile.readLine();
System.out.println(textline);
myDoc.LetterCount(textline);
myDoc.VowelCount(textline);
MyFile.close();
AdRock at 2007-11-11 22:36:02 >

# 6 Re: While loop help needed
And what happens if you do it without a while loop and put multiple MyFile.readLine()'s?
String textline = null;
textline = MyFile.readLine();
System.out.println("line 1: "+textline);
textline = MyFile.readLine();
System.out.println("line 2: "+textline);
textline = MyFile.readLine();
System.out.println("line 3: "+textline);
# 7 Re: While loop help needed
Like masher said: have you tried to do this using the Scanner class? Your loop conditional would be while( myScan.hasNext() ) ... and will not return a NullPointerException.
nspils at 2007-11-11 22:38:06 >

# 8 Re: While loop help needed
Like masher said: have you tried to do this using the Scanner class? Your loop conditional would be while( myScan.hasNext() ) ... and will not return a NullPointerException.
That shouldn't make a lot of difference. Internally, the Scanner class uses a BufferedReader as well and perhaps the OP is using a JDK < 1.5 which weren't equipped with that class. And above all that, empty lines don't return null's, as you can see:
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("Test.java");
BufferedReader buffer = new BufferedReader(file);
String line;
while((line = buffer.readLine()) != null) {
System.out.println(line);
}
}
}
The OP needs to post the full source code or post more details about the exception being thrown.
# 9 Re: While loop help needed
This is what I'm trying to do:
I have an application where I ask the user for a filename.
After the user has entered a filename the information is passed to a class which reads the file and the application can then call the object to read the file.
I intend to put other objects in the class so I don't want to put it into the application.
Here is the code I have for the app
import java.io.*;
class AnalyseDocument
{
public static InputStreamReader input = new InputStreamReader(System.in);
public static BufferedReader keyboardInput = new BufferedReader(input);
public static void main(String[] args) throws IOException
{
String fileName;
System.out.print("Enter a filename: ");
fileName = keyboardInput.readLine();
//Open the requested file for reading
FileReader file = new FileReader(fileName);
BufferedReader MyFile = new BufferedReader(file);
//Create an instance of Document
Document myDoc = new Document(fileName);
//Call the readFile object
myDoc.readFile();
MyFile.close();
}
}
Here is the code for the class file
import java.io.*;
public class Document
{
String textline;
String MyFile;
//Constructor
public Document()
{
textline = MyFile;
}
//Method for reading in the file text
public void readFile()
{
while((textline = MyFile.readLine()) !=null)
System.out.println(textline);
}
//Other objects are to be placed here for use in the application
}
AdRock at 2007-11-11 22:40:11 >

# 10 Re: While loop help needed
This is what I'm trying to do:
I have an application where I ask the user for a filename.
After the user has entered a filename the information is passed to a class which reads the file and the application can then call the object to read the file.
I intend to put other objects in the class so I don't want to put it into the application.
Here is the code I have for the app
...
Your code will never compile: you're doing MyFile.readLine(), but MyFile is a String. There is no method readLine() in the String class.
Did you try the code I posted?
# 11 Re: While loop help needed
What of that code you posted do i put in the readFile object?
Do i just out this code in?
FileReader file = new FileReader("Test.java");
BufferedReader buffer = new BufferedReader(file);
String line;
while((line = buffer.readLine()) != null) {
System.out.println(line);
AdRock at 2007-11-11 22:42:05 >

# 12 Re: While loop help needed
What of that code you posted do i put in the readFile object?
Just compile and run the entire code I posted and see how it works. Then adjust it to your needs.
Simple as that.