Results 1 to 10 of 10
- 12-13-2009, 08:04 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
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); } }
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.
-
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!
-
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. ;)
- 12-13-2009, 08:30 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
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
Last edited by JavaModo; 12-13-2009 at 08:33 PM.
- 12-13-2009, 08:37 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
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); } }
-
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.
- 12-13-2009, 08:44 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
-
Last edited by Fubarable; 12-13-2009 at 09:09 PM.
- 12-13-2009, 09:39 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
Sorry, we focused in the wrong points.
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.
-
Similar Threads
-
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 07:12 AM -
Same source file but different source folders for different build configurations?
By m3anman in forum EclipseReplies: 0Last Post: 01-29-2009, 11:43 AM -
MavenJava - browse source code of all open source projects online
By jirkacelak in forum Java SoftwareReplies: 1Last Post: 11-28-2008, 07:27 PM -
A simple Java source code viewer
By Java Tip in forum SWT TipsReplies: 0Last Post: 07-11-2008, 05:34 PM -
Problem for iReport (Data Source )
By tlgkumar in forum Advanced JavaReplies: 1Last Post: 11-27-2007, 08:39 AM
Bookmarks