Results 1 to 10 of 10
  1. #1
    JavaModo is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Lightbulb For all with a simple source problem.

    Hi,
    I just started with java yesterday and got my first problem with my source.
    The Mac OS X 10.6 terminal shows me 4 errors when I want to javac this source. The errors are about the scanner commands.

    Java Code:
    import java.util.Scanner;
    import java.io.IOException;
    
    public class Scanner
     {public static void main(String[] args)
      throws IOException
      	{int a, b, c;
    		Scanner sc = new Scanner(System.in);
    	 System.out.println("Enter a: ");
    	 a = scanner.nextInt();
    	 System.out.println("Enter b: ");
    	 b = scanner.nextInt();
    	 c = a + b;
    	 System.out.println("a+b="+c); }
     }
    Started on 12th of december 2009.
    Using a MacBook of 2008, the jEdit editor and the shell terminal. My java version is 1.6.
    Thanks for help and please forgive me possible incorrect forms, I am german.
    Last edited by JavaModo; 12-13-2009 at 08:18 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    First off, welcome to Java and welcome to the forum!

    To solve your problem: Don't name your class Scanner as this clashes with the java.util.Scanner that you are trying to use.
    Lastly, please look at my signature below about using code tags here.

    Best of luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also please see comments:
    Java Code:
          // your scanner variable is called "sc"
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter a: ");
          
          // but here you try to use an undeclared variable "scanner"
          a = scanner.nextInt();

    instead try:
    Java Code:
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter a: ");
          
          a = sc.nextInt();

    By the way: kudos on fixing your post by promptly adding code tags. Figuring out how to do this is almost a mini-intelligence test, and many get it wrong on their first try. ;)

  4. #4
    JavaModo is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks, that helped, 2 errors are gone but 2 other are still there.
    Maybe the text of my terminal can help you:
    Java Code:
    Christoph-Vetters-MacBook:beispiele Christoph$ javac Addition.java
    Addition.java:12: cannot find symbol
    symbol  : variable scanner
    location: class Addition
    	 a = scanner.nextInt();
    	     ^
    Addition.java:14: cannot find symbol
    symbol  : variable scanner
    location: class Addition
    	 b = scanner.nextInt();
    	     ^
    2 errors
    A quick question beside: How can I write a signature?
    Last edited by JavaModo; 12-13-2009 at 08:33 PM.

  5. #5
    JavaModo is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Well talking to someone helps. :D

    I solved the 2 errors by my self. Look at the code now and try it if you do not believe me:

    Java Code:
    import java.util.Scanner;
    import java.io.IOException;
    
    public class Addition
     {public static void main(String[] args)
      throws IOException
      	{int a, b, c;
    		Scanner [COLOR="Blue"]sc[/COLOR] = new Scanner(System.in);
    	 System.out.println("Enter a: ");
    	 a = [COLOR="Blue"]sc.[/COLOR]nextInt();
    	 System.out.println("Enter b: ");
    	 b = [COLOR="Blue"]sc.[/COLOR]nextInt();
    	 c = a + b;
    	 System.out.println("a+b="+c); }
     }
    Thanks very much, I will come back with more tricky problems. ;)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    See the rest of my posts above. I had already given you a solution before you posted the question. Nevertheless, you're always better off to solve it yourself. Congrats. Keep this up and you will go far.
    Last edited by Fubarable; 12-13-2009 at 08:42 PM.

  7. #7
    JavaModo is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    See the rest of my posts above. I had already given you a solution before you posted the question.
    Sorry, I didn't get this. :confused:
    Is there a opportunity to add a signature automatically on every post?

    Quote Originally Posted by Fubarable View Post
    Nevertheless, you're always better off to solve it yourself. Congrats. Keep this up and you will go far.
    Thank you. Such words in the difficult beginning help.
    Last edited by JavaModo; 12-13-2009 at 08:47 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JavaModo View Post
    Sorry, I didn't get this. :confused:
    Read all the posts in the thread above and you'll get it.

    As for your signature, just press the "Quick Links" link above and then "Edit Signature".
    Last edited by Fubarable; 12-13-2009 at 09:09 PM.

  9. #9
    JavaModo is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Sorry, we focused in the wrong points.

    Quote Originally Posted by Fubarable View Post
    See the rest of my posts above. I had already given you a solution before you posted the question.
    I thought this was the answer of my question about the signature.

    So my part: "I didn't get it." was, because I thought you had given me the solution for my signature problem.

    Misunderstanding of the year :D!!

    Thanks now for the signature answer. See, it works.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  2. Replies: 0
    Last Post: 01-29-2009, 11:43 AM
  3. Replies: 1
    Last Post: 11-28-2008, 07:27 PM
  4. A simple Java source code viewer
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:34 PM
  5. Problem for iReport (Data Source )
    By tlgkumar in forum Advanced Java
    Replies: 1
    Last Post: 11-27-2007, 08:39 AM

Tags for this Thread

Posting Permissions

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