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 17 of 17

Thread: new use of bufferedreader, uncharted territory

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default new use of bufferedreader, uncharted territory

    I was trying to write part of a program to read in lines from a text file (in the same directory as the .java and compiled class) and print out those lines. I know a little java, but not much, and I don't have the expertise to figure out what is wrong with this code.

    import java.io.*;
     
    public class Prog1{
      public static void main(String args[]){
        try { 
        BufferedReader in = new BufferedReader(new FileReader("testtext.txt"));
        String line = in.readLine();
        in.close();
        } catch (FileNotFoundException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); }
     
        if(line==null){
     System.out.println("reading process done");
        } else {
        System.out.println(line);
        }
      }
    }
    Last edited by helloworld922; January 20th, 2012 at 10:03 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    Why do you think there is anything wrong with the code?
    Can you explain?

  3. The Following User Says Thank You to Norm For This Useful Post:

    Goldfinch (January 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory

    Sorry, don't know how this works, there were some compile errors with the symbols Level and Logger both times they appeared. At one point, I tried defining those variables but that did not work either, then it assumed that SEVERE was a variable.
    Last edited by Goldfinch; January 20th, 2012 at 10:19 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    there were some compile errors
    Please copy and paste the full text of the errors here if you want help with them.
    Why are you using Level and Logger?

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory

    Here were the compiler errors:


    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
    symbol : variable Logger
    location: class Prog1
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
    symbol : variable Level
    location: class Prog1
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
    symbol : variable Logger
    location: class Prog1
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
    symbol : variable Level
    location: class Prog1
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 11]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:11: cannot find symbol
    symbol : variable line
    location: class Prog1
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 14]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:14: cannot find symbol
    symbol : variable line
    location: class Prog1

    The unfamiliar classes come from the logger class java.util.logging.Logger

    void log(Level level, String msg, Throwable thrown)
    Log a message, with associated Throwable information.

    static Logger getLogger(String name)
    Find or create a logger for a named subsystem.

    log(Level level, String msg, Throwable thrown)
    Log a message, with associated Throwable information.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    You need to tell the compiler where to find the definitions for those classes. You can use an import statement to do that.

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory

    so I tried some changes and commented out my print statements near the end (since that's a separate mess there):
    import java.io.*;
    import java.util.logging.Logger;
     
    public class Prog1{
      public static void main(String args[]){
        try { 
        BufferedReader in = new BufferedReader(new FileReader("testtext.txt"));
        String line = in.readLine();
        in.close();
        } catch (FileNotFoundException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); }
     
    //  if(line==null){
    //  System.out.println("reading process done");
    //   } else {
    //   System.out.println(line);
        }
      }
    }

    That yielded only one error (also, it no longer had a problem identifying Logger). This was the error:

    #Compilation completed. The following files were not compiled:
    1 error found:
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 18]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:18: class, interface, or enum expected

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    What is on or before line 18?
    Make sure the {}s are correctly paired.

  10. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory


  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    is it appropriate for me to ask this many questions?
    Definitely, yes.

    Where is the Level variable defined? If it is from the Java SE classes, read the API doc to find out what package it is in and add an import for it.

  12. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory

    That worked, and the program compiled. I found a way to modify the program further, supposedly so it can print its inputs from the file it reads. That created some additional challenges. Earlier I had those system.out.println statements that I called a "mess," but commented out so the program would compile.

    import java.io.*;
    import java.util.logging.Logger;
    import java.util.logging.*;
     
    public class Prog1{
      public static void main(String args[]){
        File file = new File("test.txt");
        StringBuffer contents = new StringBuffer();
     
        try { 
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = in.readLine()) != null) {
                    contents.append(line)
                        .append(System.getProperty(
                            "line.separator"));
        } catch (FileNotFoundException ex) { 
          Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (IOException ex) { 
          Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); 
        } finally {
          try {
            if (in != null) {
                        in.close();
                    }
         } catch (IOException ex) {
           Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
                }
     
         System.out.println(contents.toString());
      }
    }
      }}

    I got ten errors this time, it looks like all of the errors are related.

    10 errors found:
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 10]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:10: 'try' without 'catch' or 'finally'
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: 'catch' without 'try'
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: not a statement
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: ')' expected
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: ';' expected
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: 'catch' without 'try'
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: not a statement
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: ')' expected
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: ';' expected
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 21]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:21: 'finally' without 'try'

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    Again check for correctly matching {}s

  14. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory

    Thanks, that worked and I've also learned a lot about how to read the compiler errors too.

    import java.io.*;
    import java.util.logging.Logger;
    import java.util.logging.*;
     
    public class Prog1
    {
      public static void main(String args[])
      {
        File file = new File("test.txt");
        StringBuffer contents = new StringBuffer();
     
        try { 
          //first block of inserted code
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = in.readLine()) != null) { contents.append(line).append(System.getProperty("line.separator")); } 
         //end first block of inserted code
        } catch (FileNotFoundException ex) {
          Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
        } finally 
        {
          try {
            if (in != null){ in.close(); } //block of insered code
          } catch (IOException ex) { 
            Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); 
          }
        }    
         System.out.println(contents.toString());
      } //end main method
    }//end class

    So this time it doesn't seem to be an import related problem, an object used earlier in the code is not recognized twice, although it does exist within a nested try-catch within the main one where the instance of the object was created.

    2 errors found:
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 24]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:24: cannot find symbol
    symbol : variable in
    location: class Prog1
    File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 24]
    Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:24: cannot find symbol
    symbol : variable in
    location: class Prog1

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    The problem is the scope of the definition for the variable. The definition is only known within the enclosing pair of {}s. You need to move the definition up until it is at the same nesting level of {}s where you are trying to use it. You can define it in one place (and give it a null value) and give it a value in another place.

  16. #15
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: new use of bufferedreader, uncharted territory

    At this point it compiled fine, but no values seem to be passed to the println statement at the bottom of the class since the console isn't printing the values in the text file to the console.

    import java.io.*;
    import java.util.logging.Logger;
    import java.util.logging.*;
     
    public class Prog1
    {
      public static void main(String args[])
      {
        File file = new File("test.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader in = null;
        String line = null;
        try { 
          //first block of inserted code
        in = new BufferedReader(new FileReader(file));
        while ((line = in.readLine()) != null) { contents.append(line).append(System.getProperty("line.separator")); } 
         //end first block of inserted code
        } catch (FileNotFoundException ex) {
          Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); 
        } catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
        } finally 
        {
          try {
            if (in != null){ in.close(); } //block of insered code
          } catch (IOException ex) { 
            Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); 
          }
        }    
         System.out.println(contents.toString());
      } //end main method
    }//end class
    Last edited by Goldfinch; January 21st, 2012 at 07:10 PM.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: new use of bufferedreader, uncharted territory

    Add more printlns to show the values of the variables as they are changed. For example print out the value of line immediately after each readLine() statement.

  18. #17
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thank You. It worked out.

Similar Threads

  1. 'BufferedReader' problem...
    By Akarsh in forum Member Introductions
    Replies: 3
    Last Post: August 15th, 2011, 05:37 PM
  2. BufferedReader
    By reg4ltip in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2010, 12:15 PM
  3. BufferedReader error?
    By Umogrim in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 28th, 2010, 08:51 PM
  4. Problem in BufferedReader
    By shamed in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 1st, 2009, 04:30 PM
  5. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 09:44 AM