This discussion is archived
9 Replies Latest reply: May 4, 2009 4:22 AM by 807588 RSS

BufferedReader problem

807588 Newbie
Currently Being Moderated
I am reading in a text file that has 9 items on 9 lines, however after reading the contents of the file and printing out what has been read by the BufferedReader, only lines 1,3,5 & 7 are being printed. The file format is as follows;
0 0 0 7 0 0 2 0 6
0 0 3 2 0 0 5 0 0
7 0 0 0 8 0 0 0 9
6 5 0 0 0 1 0 0 0
0 0 2 0 0 0 0 0 0
0 0 0 5 0 6 0 4 3
9 0 0 0 6 0 0 0 7
0 0 4 0 0 7 9 0 0
0 0 0 0 0 9 0 0 0
The java code is as follows;
if (button == load){
            try{
                 JFileChooser fc = new JFileChooser();//new dialog
                 File selectedFile;
                 BufferedReader in;
                 FileReader reader = null;
                 fc.setCurrentDirectory(new File("." + "\\Puzzles"));
                             
                 //opens dialog if button clicked
                 if (fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
                      //gets file from dialog
                      selectedFile = fc.getSelectedFile();
                      //makes sure files can be processed before proceeding
                      if (selectedFile.canRead() && selectedFile.exists()){
                           reader = new FileReader(selectedFile);                           
                      }
                 }
                 in = new BufferedReader(reader);
                 String inputLine = null;
                 int lineCounter = 0;
                 
                 while((inputLine = in.readLine())!= null && lineCounter < 10){                
                     lineCounter ++;                     
                         System.out.println("Line " + lineCounter + ":" + inputLine);
                         inputLine = in.readLine();                  
                           dialog.setText("Status: Puzzle Loaded!");
               }
               in.close();
               System.out.println("File Closed");
               }
                //catches input/output exceptions and all subclasses exceptions
                catch(IOException ex){
                     System.out.println("Error Processing File:\n" + ex.getMessage()+"\n");
                }
                //catches nullpointer exception, file not found
                catch(NullPointerException ex){
                     System.out.println("Open File Cancelled:\n" + ex.getMessage()+"\n");
                }
           }
Any help would be appreciated
  • 1. Re: BufferedReader problem
    800345 Newbie
    Currently Being Moderated
    You are reading in a line twice. Once in the while(...) construct and once in the body of the while loop. The second one you don't do anything with the file. You should probably get rid of this second in.readLine() statement.
  • 2. Re: BufferedReader problem
    699554 Newbie
    Currently Being Moderated
    while((inputLine = in.readLine())!= null && lineCounter < 10){                
      lineCounter ++;                     
      System.out.println("Line " + lineCounter + ":" + inputLine);
      inputLine = in.readLine();                  
      dialog.setText("Status: Puzzle Loaded!");
    }
    Remove the second inputLine from your while loop, you only want to read one line at a time.
     inputLine = in.readLine();
    Mel
  • 3. Re: BufferedReader problem
    807588 Newbie
    Currently Being Moderated
    while((inputLine = in.readLine())!= null && lineCounter < 10)
    {                
                         lineCounter ++;                     
                             System.out.println("Line " + lineCounter + ":" + inputLine);
                             inputLine = in.readLine();                  
                               dialog.setText("Status: Puzzle Loaded!");
                   }
    
      
    What is the functionality of the line 5 i.e. inputLine = in.readLine();.You are already reading the line at the start of the while loop.Whay again???

    Edited by: TechnicalGuy7 on May 4, 2009 4:21 AM
  • 4. Re: BufferedReader problem
    807588 Newbie
    Currently Being Moderated
    Thanks steve, mel! Just me being stupid, should have spotted that one. Tested it and works a treat.
  • 5. Re: BufferedReader problem
    807588 Newbie
    Currently Being Moderated
    r006060 close the thread by putting it as answered if the problem is rectified.
  • 6. Re: BufferedReader problem
    699554 Newbie
    Currently Being Moderated
    r006060 wrote:
    Thanks steve, mel! Just me being stupid, should have spotted that one. Tested it and works a treat.
    YW - I replied within 20 seconds of seeing your post and steve still posted before me lol

    Mel
  • 7. Re: BufferedReader problem
    807588 Newbie
    Currently Being Moderated
    Sorry to be a pain, I'm unsure of how to go about that. I have never done it before.

    Thanks again
  • 8. Re: BufferedReader problem
    699554 Newbie
    Currently Being Moderated
    r006060 wrote:
    Sorry to be a pain, I'm unsure of how to go about that. I have never done it before.

    Thanks again
    Ingore him, you never marked your thread as a question so you are fine bud :).

    Mel
  • 9. Re: BufferedReader problem
    807588 Newbie
    Currently Being Moderated
    Okay. Thanks for your help again!