error with "Scanner" class in basic calculator program.
Hello all, I am new to the community so sorry if I don't post the correct way. Will do the best that I can. :)
Here is my code.I am using a IDE (eclipse) and it doesn't show me any errors. But every time I try to run it in CMD but I get a error saying "cannot find symbol class in = new Scanner" Any help would be much appreciated!
Code Java:
public class HelloApp
{
public static void main(String [] args)
{
Scanner in = new Scanner( System.in);
int number1;
int number2;
int sum;
System.out.println("Hello!");
System.out.println("Welcome to my first java calculator!");
System.out.println("When you are ready, Input your first number!");
number1 = input.nextInt(); // User inputs first number
System.out.println("Okay, Now the second number please!");
number2 = input.nextInt(); // User inputs second number
sum = number1 + number2;
System.out.printf ("Sum is %d\n", sum);
}
}
Very basic program so I doubt that anyones brain will be hurt over this, I am jsut so new that every key is confusing.
Re: error with "Scanner" class in basic calculator program.
just realized my indentation is off when i copy pasted it to the thread. indention is correct in actual file. sorry.
Re: error with "Scanner" class in basic calculator program.
HelloApp is the name of the class you wrote.
Scanner is the name of a class you did not write but are attempting to use.
System is the name of a class you did not write but are attempting to use.
System is included by default. Scanner is not. You have to import the Scanner class to use it.
import java.util.Scanner;
Re: error with "Scanner" class in basic calculator program.
Got it working thanks jps! quick question on the import of scanner. When importing it do I always have to put it before the class I am going to be using it in? or can I put the import statement withing the class brackets?
Re: error with "Scanner" class in basic calculator program.
Quote:
Originally Posted by
wheezebeez
quick question...can I put the import statement within the class brackets...
What happens when you try it like that? Isn't that a quicker way to find the answer?
Cheers!
Z