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

Thread: cannot find symbol

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    India
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default cannot find symbol

    i have programmed two programs and both of them when compiled highlighted the same error:Scanner ob=new Scanner(System.in);.saying cannot find symbol

    void input()
    {
    Scanner ob=new Scanner(System.in);
    System.out.print("Enter the first number:");
    x=ob.nextInt();
    System.out.println("Enter second number:");
    y=ob.nextInt();
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: cannot find symbol

    Is the import statement missing?
    If you don't understand my answer, don't ignore it, ask a question.

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

    ATIBA SHEIKH (December 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    India
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: cannot find symbol

    umm...oh yes.it just slipped off from my mind...thanks

    --- Update ---

    but now again it says can't find symbol.."ch=ob.nextInt();"


    int ch;
    do
    {

    System.out.println("Press 1:ADDITION");
    System.out.println("Press 2:SUBTRACTION");
    System.out.println("Press 3:MULTIPLICATION");
    System.out.println("Press 4IVISION");

    System.out.println("ENTER YOUR CHOICE:");
    ch=ob.nextInt();

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: cannot find symbol

    it says can't find symbol
    Please copy the full text of the error message and post it here.
    What you posted does not say what symbol cannot be found.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: cannot find symbol

    you have to import java.util.Scanner;
    then code runs well

  7. The Following User Says Thank You to akymcs For This Useful Post:

    ATIBA SHEIKH (December 22nd, 2012)

  8. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: cannot find symbol

    Quote Originally Posted by akymcs View Post
    you have to import java.util.Scanner;
    then code runs well
    How do you know this? For all we know, his ob variable could be local to a block and not visible from where he's trying to use it. I second Norm's recommendation that we get more complete information before we start shooting from the hip.

  9. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: cannot find symbol

    Quote Originally Posted by curmudgeon View Post
    How do you know this? For all we know, his ob variable could be local to a block and not visible from where he's trying to use it. I second Norm's recommendation that we get more complete information before we start shooting from the hip.
    i wrote for this..
    error:Scanner ob=new Scanner(System.in);.saying cannot find symbol

    now the next what hi say can not find symbol it will require complete code...

  10. #8
    Junior Member
    Join Date
    Dec 2012
    Location
    India
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: cannot find symbol

    sorry for this.here's the code:

    import java.util.*;
    class calculator
    {
    int x,y,result;
    {
    x=0;
    y=0;
    result=0;

    }
    void input()
    {

    Scanner ob=new Scanner(System.in);
    System.out.print("Enter the first number:");
    x=ob.nextInt();
    System.out.println("Enter second number:");
    y=ob.nextInt();
    }

    void sum()
    {
    result=x+y;
    }
    void sub()
    {
    result=x-y;
    }

    void mult()
    {
    result=x*y;
    }

    void div()
    {
    result=x/y;
    }

    void display()
    {
    System.out.println("Sum of the numbers:"+result);
    System.out.println("Difference between the numbers:"+result);
    System.out.println("product of the numbers:"+result);
    System.out.println("division of the numbers:"+result);
    }
    class main
    {

    public static void main(String args[])
    {
    calculator ob=new calculator();
    int ch;
    do
    {

    System.out.println("Press 1:ADDITION");
    System.out.println("Press 2:SUBTRACTION");
    System.out.println("Press 3:MULTIPLICATION");
    System.out.println("Press 4IVISION");

    System.out.println("ENTER THE CHOICE:");
    ch=ob.nextInt();
    if(ch==1)
    {
    ob.input();
    ob.sum();
    ob.display();
    }

    if(ch==2)
    {
    ob.input();
    ob.sub();
    ob.display();
    }
    if(ch==3)
    {
    ob.input();
    ob.mult();
    ob.display();
    }
    if(ch==4)
    {
    ob.input();
    ob.div();
    ob.display();
    }

    System.out.println("ENTER THE CHOICE:");
    ch=ob.nextInt();
    }
    while(n<5);
    }
    }
    }

  11. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: cannot find symbol

    In the class main:
    ch=ob.nextInt();
    ob is a calculator object and don't recognize the methode nextInt() who belongs to a Scanner object.

    while(n<5);
    the variabele n is not declared in the main class.

  12. The Following User Says Thank You to alaindhaene For This Useful Post:

    ATIBA SHEIKH (December 23rd, 2012)

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: cannot find symbol

    Can you post the full text of the error message that shows the line number of the line with the error?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: cannot find symbol

    ATIBA nextInt is method of Scanner class..so whenever you use nextInt you must have to create object of Scanner class then use can use nextInt. In your class main there is no any object of Scanner class so you cannt use nextInt method.
    so you have to modify your code something like this

    class main
    {

    public static void main(String args[])
    {
    calculator ob=new calculator();
    Scanner ob1 = new Scanner(); //create Scanner class object. Now ob1 can use to call nextInt method of Scanner class.
    int ch;
    do
    {

    System.out.println("Press 1:ADDITION");
    System.out.println("Press 2:SUBTRACTION");
    System.out.println("Press 3:MULTIPLICATION");
    System.out.println("Press 4IVISION");

    System.out.println("ENTER THE CHOICE:");
    ch=ob1.nextInt();
    if(ch==1)
    {
    ob.input();
    ob.sum();
    ob.display();
    }
    ...... rest of code...

  15. The Following User Says Thank You to akymcs For This Useful Post:

    ATIBA SHEIKH (December 25th, 2012)

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: cannot find symbol

    @akymcs
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #13
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cannot find symbol

    are u using jdk version below 1.4 which does not support scanner class
    use bufferedReader instead of that

Similar Threads

  1. [SOLVED] Cannot Find Symbol
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2012, 02:46 PM
  2. cannot find symbol
    By lanpan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2012, 07:13 AM
  3. Cannot find symbol
    By waarten in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 11th, 2012, 02:15 PM
  4. Cannot find Symbol?
    By defmetalhead in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 5th, 2011, 08:48 AM
  5. Cannot find symbol?
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 10th, 2010, 09:02 PM