Results 1 to 9 of 9
  1. #1
    Sobutai is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default Buffered Writter While Loop

    EDIT: Newer code below



    Thank you for your time.
    Last edited by Sobutai; 05-11-2011 at 02:13 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I have a try catch where the catch is looping forever, leading me to believe that the while loop is for some reason not incrementing.

    If you want the increment to always happen, put it in the finally part of try/catch/finally.

  3. #3
    Sobutai is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    If you want the increment to always happen, put it in the finally part of try/catch/finally.
    I changed the while loop to a for loop that increments ( for(int i = 1; i <= numrows; i++) and added a finally that increments if the try doesn't work. Now my catch still goes off but it only goes off for as many rows as I have in my database. (3)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You should not increment twice. If you increment as part of the for statement, then all the catch should do is continue.

    With so much catching going on it's not completely clear what the problem is exactly. Perhaps you could post the code you are using now and describe the actual behaviour (in objective terms: ie what gets written to out.)

  5. #5
    Sobutai is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    I've been able to fix the majority of my problems on my own, but now I'm having a huge problem. Once I figure it out however, I'm done.

    I have several stringer getting fed information in a for loop, that information gets wrote into a text file then it is suppose to go down a line on the text file. However, it only wants to write the last string entered and never go down a line and write there.

    Java Code:
    private void mnu1Save_Click() {
    
            
    try{
             
    
             rs.last();
             rs.moveToCurrentRow();
             int numrows = rs.getRow();
             rs.first();
             rs.moveToCurrentRow();
    
            
            for(int i = 1; i <= numrows; i++){ 
             try{
              rs = stmt.executeQuery("SELECT Last, First, Street, City, State, Zip, Phone FROM MailList");
          
          //Sets the cursor to the first entry
          rs.absolute(i);
                    
          //Sets the strings to the information in specific row and column
         
          String Last = rs.getString("Last");
          String First = rs.getString("First");
          String Street = rs.getString("Street");
          String City = rs.getString("City");
          String State = rs.getString("State");
          String Zip = rs.getString("Zip");
          String Phone = rs.getString("Phone");
          
          String Total = Last + "," + First + "," + Street + "," +  City + "," + State + "," +Zip + "," +Phone;
         
          FileWriter fstream = new FileWriter("out.txt");
          BufferedWriter out = new BufferedWriter(fstream);
          out.write( Total );
          out.newLine();
          out.close();
    
    
                rs.next();
                 
                 }
                
            
          
            catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
       }
    }
     catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        } 
    }
    The output is suppose to say:
    Morgan,Harry,123 Lotus Lane,Schenectady,NY,12345,518-445-4554
    Trump,Donald,Borgata Room 999,Atlantic City,NJ,54321,305-445-5445
    null,null,null,null,null,null,null

    Only shows:
    null,null,null,null,null,null,null

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default

    That's expected, since you construct a new writer, write each line and then close the writer. Each time you write a line it overwrites the file created for the previous line.

    Open the writer before the for loop and close it in the finally block (with a null check).

    And fix your indentation if you expect people here to read your code.
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  7. #7
    Sobutai is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    That's expected, since you construct a new writer, write each line and then close the writer. Each time you write a line it overwrites the file created for the previous line.

    Open the writer before the for loop and close it in the finally block (with a null check).

    And fix your indentation if you expect people here to read your code.
    Code Conventions for the Java(TM) Programming Language: Contents

    db

    I apologize. I have fixed my code (and hopefully my indentations)is this what you meant:

    Java Code:
    private void mnu1Save_Click() {
     String[] writeArray = new String[100];
             
         try{
             
    
         rs.last();
         rs.moveToCurrentRow();
         int numrows = rs.getRow();
         rs.first();
         rs.moveToCurrentRow();
    
         FileWriter fstream = new FileWriter("out.txt");
         BufferedWriter out = new BufferedWriter(fstream);
               for(int i = 1; i <= numrows; i++){ 
                  try{
              rs = stmt.executeQuery("SELECT Last, First, Street, City, State, Zip, Phone FROM MailList");
          
              //Sets the cursor to the first entry
               rs.absolute(i);
                    
               //Sets the strings to the information in specific row and column
         
          
                 Newstring[0] = rs.getString("Last");
                 Newstring[1] = rs.getString("First");
                 Newstring[2] = rs.getString("Street");
                 Newstring[3] = rs.getString("City");
                 Newstring[4] = rs.getString("State");
                 Newstring[5] = rs.getString("Zip");
                 Newstring[6] = rs.getString("Phone");
          
                 String concat = Newstring[0] + "," + Newstring[1]+ + "," +Newstring[2] + "," + Newstring[3] + "," + Newstring[4] + "," + Newstring[5]  + "," + Newstring[6] "\n";
          
                 writeArray[i] = concat;
            
          
                 out.write( writeArray[i] );
                 out.newLine();
          
          
                   rs.next();
                 
                 }
                
            
            
          
               catch (Exception e){
               System.err.println("Error: " + e.getMessage());
                }
                finally {
                if (out != null) { 
                out.close();
                 }
         }
       }
      }
       catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
        } 
    }
    I am now getting a Stream Closed Error in the Terminal Window.
    Last edited by Sobutai; 05-11-2011 at 04:08 AM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default

    You're still closing the writer inside the loop.

    db

  9. #9
    Sobutai is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    If I put the finally block with the other catch block it tells me it cannot find variable out.

    EDIT: Added another Try Catch for the writer, it works perfectly. Thank you!
    Last edited by Sobutai; 05-11-2011 at 04:30 AM.

Similar Threads

  1. Buffered reader
    By franckboy in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 07:16 PM
  2. Developing Word Writter and Formatter
    By Anchal in forum AWT / Swing
    Replies: 1
    Last Post: 04-06-2010, 03:35 AM
  3. Buffered Reader
    By ilovepolluting in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 09:16 AM
  4. hey everyone a quick buffered writter question
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 11:44 AM

Posting Permissions

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