Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: The simpler the program - seemingly the more scope for error :p

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default The simpler the program - seemingly the more scope for error :p

    Hi again,

    Just thought I'd check with you guys to see where I've gone wrong with this simple I/O program. Having just done quite a bit of reading on I/O I thought would write myself I nice little program from scratch to test some of the said principles out. Sadly my output is locked within an infinite loop when I try reading from it.

    The first code dump is the main prog, it validates whether there is an output txt file created, and if not creates one. Thus on it's 2nd execution it will instantiate an OutputCheck object + invoke it's checkOutput() method to see whether the output txt file originally created fits the bill. But it's an endless loop as previously mentioned.




     
    import java.io.*;
     
     
    public class StringFileReadWrite {
     
        public static void main (String [] args){
     
     
     
            File inFile = new File("src\\input.txt");
            File outFile = new File("src\\output.txt");
     
            if(!(outFile.exists())){
     
            try{
                FileReader fr = new FileReader(inFile);
                BufferedReader br = new BufferedReader(fr);
     
                FileWriter fw = new FileWriter(outFile);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter pw = new PrintWriter(bw);
     
                String line = br.readLine();
     
                while(line != null){
                    pw.println(line);
                    br.readLine();
     
                }
     
     
     
                br.close();
                pw.close();
                System.out.println("The File Operation has been completed");
     
            }catch(IOException e){
                System.out.println(e);
     
                }
            }
     
                else{
                        OutputCheck oc = new OutputCheck();
                        oc.checkOutput();
     
                    }
     
     
     
        }
     
    }

     
    import java.util.Scanner;
    import java.io.*;
     
    public class OutputCheck {
     
            File checked = new File("src\\output.txt");
     
            public void checkOutput(){
            try{
     
                Scanner scan = new Scanner(checked);
     
                while(scan.hasNext()){
                    String str = scan.next();
                    System.out.println(str);
                    }
     
                    scan.close();
     
                }catch(IOException e){
                }finally{}
     
            }
     
    }


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: The simpler the program - seemingly the more scope for error :p

    Is this what you wanted?

    import java.io.*;
     
    public class StringFileReadWrite {
     
        public static void main (String [] args){
            File inFile = new File("src\\input.txt");
            File outFile = new File("src\\output.txt");
     
            if(!outFile.exists()){
            	try{
            		BufferedReader readInput = new BufferedReader(new FileReader(inFile));
            		PrintWriter writeOutPut = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
     
            		String line ="";
     
            		while((line = readInput.readLine()) != null) {
            			writeOutPut.println(line);
            		}
     
            		readInput.close();
            		writeOutPut.close();
            		System.out.println("The File Operation has been completed");
            	}catch(IOException e){
            		System.out.println(e);
            	}
            } else{
            	OutputCheck oc = new OutputCheck();
                oc.checkOutput();
     
            }
        }
     
    }

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: The simpler the program - seemingly the more scope for error :p

    Quote Originally Posted by bluurr View Post
    Is this what you wanted?

    ...
    Ah, I like your efficiency with the anonymous classes instead of one big lump of chained code - definitely will keep a mental note of that cheers However, no dice I'm afraid - I was more interested in the reason(s) behind my output being in an infinite loop. Any thoughts on that btw?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: The simpler the program - seemingly the more scope for error :p

    String line = br.readLine();
     
                while(line != null){
                    pw.println(line);
                    br.readLine();
     
                }
    This will never terminate because you never change the value of line. Thus, unless on the first read line == null, you'll be stuck in an infinite loop.

    Try running your program in debug mode and step through it one line of code at a time, or better yet pretend you're the computer and try running the code with a piece of paper to keep track of variable values.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: The simpler the program - seemingly the more scope for error :p

    Quote Originally Posted by helloworld922 View Post
    This will never terminate because you never change the value of line. Thus, unless on the first read line == null, you'll be stuck in an infinite loop.

    Try running your program in debug mode and step through it one line of code at a time, or better yet pretend you're the computer and try running the code with a piece of paper to keep track of variable values.
    hmm, ok. So I realise that the condition of the while loop remains static - and thus will never terminate. But I'm not to sure of how to change this.


    My first guess was to Change my code to this ( which seems more logical) but the condition still remains constant forever it would seem...


     while ((line = br.readLine()) != null) {
     
    pw.println(line);
     
    }



    **This may be a bit of a shot in the dark, but I'm wondering if it has something to do with my input text file...

  6. #6
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: The simpler the program - seemingly the more scope for error :p

    Quote Originally Posted by Bacon n' Logic View Post

    **This may be a bit of a shot in the dark, but I'm wondering if it has something to do with my input text file...
    Having put a '\n' at the end of my input text file - the problem seems resolved - albeit with the '\n' now being visible on output. Is this the intended solution, Or is there another way?

Similar Threads

  1. What's the damn error in this program...
    By bsudhir6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 12th, 2010, 07:57 AM
  2. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  3. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM
  4. Need help with program please: getting symbol error
    By blinkzz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 18th, 2009, 02:23 AM
  5. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM