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

Thread: Problem in BufferedReader

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Problem in BufferedReader

    This was given as a quiz to us by our instructor. She gave me perfect score for this, but when I actually run the program on my PC it says "cannot resolve symbol". What does it means? How could I fix it? Can anyone please help me? Thank you very much!


    import java.io.*;
    public class BufferedReader
    {
        public static void main (String[]args)
        {
            BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
     
            String num;
     
            try
            {
                num = dataIn.readLine();
                int number1 = Integer.parseInt(num);
                num = dataIn.readLine();
                int number2 = integer.parseInt(num);
            }
     
            catch (IOException e)
            {
                System.out.println("Error!");
            }        
     
            int sum = number1 + number2;
            System.out.println("The Sum is " +sum);
        }    
    }

    EDIT:

    By the way, I'm using NetBeans IDE 5.0


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Problem in BufferedReader

    Firstly you named your class BufferedReader, so when you do new BufferedReader, Java is looking for the contructor within your class not the BufferedReader default library. Try renaming to something like "BufferedReaderExample".

    Secondly, you define two integers within a try statement, and then attempt to use them outside of the try statement, but this is not possible due to scoping laws.

    Regards,
    Chris

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Problem in BufferedReader

    Thank you for the reply, sir Chris. I've already renamed my class. Now the last two lines encountered the same error. I'm sorry, I really don't know about this. I just followed the instructions of my teacher.

      int sum = number1 + number2;
     System.out.println("The Sum is " +sum);

    It says:
    C:\Documents and Settings\shamed\javaprogs\src\BuffReadwww.java:35: cannot resolve symbol

    symbol : variable number1

    location: class BuffReadwww

    int sum = number1 + number2;

    C:\Documents and Settings\shamed\javaprogs\src\BuffReadwww.java:35: cannot resolve symbol

    symbol : variable number2

    location: class BuffReadwww

    int sum = number1 + number2;





    The system is out of resources.

    Consult the following stack trace for details.

    java.lang.OutOfMemoryError

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem in BufferedReader

    The reason for the symbol issue is that your variables number1 and number2 are declared inside the try clause so you will not be able to reach them outside.

    Just move the last two lines into the try/catch.

    try
            {
                num = dataIn.readLine();
                int number1 = Integer.parseInt(num);
                num = dataIn.readLine();
                int number2 = integer.parseInt(num);
     
                int sum = number1 + number2;
                System.out.println("The Sum is " +sum);
            } catch (IOException e) {
                System.out.println("Error!");
            }

    // Json

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Problem in BufferedReader

    It's working now. Thank you sir Chris and sir Json.

Similar Threads

  1. 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
  2. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM
  3. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM