Results 1 to 7 of 7
- 07-21-2012, 06:58 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Java IO help please. program doesnt end
Hello, i am learning the java IO class currently. i was able to create and also append content to the file. then i was trying to open the file and display the content of the file in the console. i was able to do that too . however the program keep on displaying the content of the first line and never stops until i force stop. It would be really great if someone can correct my code. Here is the code.
import java.util.Scanner;
import java.io.*;
public class filereader {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
FileWriter fw = new FileWriter("c:/java/new.txt", true);
PrintWriter append = new PrintWriter(fw);
Scanner in = new Scanner(System.in);
System.out.println("Enter Your name..");
String name = in.nextLine();
append.println(name);
fw.close();
in.close();
FileReader fr = new FileReader("c:/java/new.txt");
BufferedReader br = new BufferedReader(fr);
String data = br.readLine();
while (data != null) {
System.out.println(data);
}
br.close();
}
}
- 07-21-2012, 08:09 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
-
Re: Java IO help please. program doesnt end
You've got an interesting while loop:
If you logically walk through your code, you should be thinking, where does data change inside of the while loop? If data doesn't change, how will the loop ever end?Java Code:String data = br.readLine(); while (data != null) { System.out.println(data); }
After noticing this, your fix should be trivial, and I'll leave it to you to implement.
- 07-21-2012, 08:27 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Re: Java IO help please. program doesnt end
Thanks a lot both of you. That really helped and i fixed my code.
this is how i fixed.
while (data != null) {
System.out.println(data);
data = br.readLine();
}Last edited by peshal; 07-21-2012 at 08:28 PM. Reason: add code
- 07-21-2012, 08:36 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Java IO help please. program doesnt end
Well done; here's a more idiomatic way of writng the same:
kind regards,Java Code:while ((data= br.readLine()) != null) { System.out.println(data); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-22-2012, 01:22 PM #6
Re: Java IO help please. program doesnt end
Why do they call it rush hour when nothing moves? - Robin Williams
- 07-22-2012, 04:00 PM #7
Member
- Join Date
- Jun 2012
- Location
- Ohio
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Applet doesnt start or it doesnt show
By 3dprogger in forum Java AppletsReplies: 2Last Post: 01-07-2011, 07:03 PM -
This program compiles but doesnt run properly!
By ErikD99 in forum New To JavaReplies: 5Last Post: 12-03-2010, 08:44 PM -
Method Doesnt turn negative number 0 and doesnt return 3
By anonb in forum New To JavaReplies: 7Last Post: 09-28-2010, 12:17 AM -
JCreator doesnt have Java ME, can I install it?
By Addez in forum JCreatorReplies: 1Last Post: 11-22-2009, 10:38 AM -
My program doesnt display anything
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 10-19-2007, 02:50 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks