Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Java IO help please. program doesnt end

  1. #1
    peshal is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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();

    }

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java IO help please. program doesnt end

    Quote Originally Posted by peshal View Post
    Java Code:
    while (data  != null) {
    	System.out.println(data);	
    }
    This loop prints the same data over and over again; after printing you should read a next line.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java IO help please. program doesnt end

    You've got an interesting while loop:

    Java Code:
    String data = br.readLine();
    while (data != null) {
       System.out.println(data);	
    }
    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?

    After noticing this, your fix should be trivial, and I'll leave it to you to implement.

  4. #4
    peshal is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java IO help please. program doesnt end

    Well done; here's a more idiomatic way of writng the same:

    Java Code:
    while ((data= br.readLine()) != null) {
       System.out.println(data);
    }
    kind regards,

    Jos
    peshal likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Java IO help please. program doesnt end

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: Java IO help please. program doesnt end

    Your issue is in the while loop. That loop will continue to run until data is null. Which it will never be.

Similar Threads

  1. Applet doesnt start or it doesnt show
    By 3dprogger in forum Java Applets
    Replies: 2
    Last Post: 01-07-2011, 07:03 PM
  2. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 08:44 PM
  3. Replies: 7
    Last Post: 09-28-2010, 12:17 AM
  4. Replies: 1
    Last Post: 11-22-2009, 10:38 AM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •