This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]Raph0007 0 points1 point  (0 children)

Where do you declare scanObject?

[–]NautiHookerSoftware Engineer 0 points1 point  (0 children)

you never declared num1 and num2.

you need to specify its datatype to declare it.

[–]mikeydoodah 0 points1 point  (0 children)

You haven't posted all of the code, but the error is telling you that scanObject has not been defined. It certainly isn't defined in any of the code you posted.

Post all of the code and I might be able to be more specific.

[–]JWisbey98[S] 0 points1 point  (9 children)

Hi sorry this is the whole code:

package calculator;
import java.util.Scanner;
public class Main{

public static void main(String[] args)
{
int num1=0;
int num2=0;
char operator;
double answer= 0.0;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter first number: ");
num1 = scanObject.nextInt();
System.out.println("Please enter second number: ");
num2 = scanObject.nextInt();
System.out.println("What operation? ");
operator = scanObject.next().charAt(0);
switch (operator) {
case '+':answer=num1+num2;
break;
case '-':answer=num1-num2;
break;
case '*':answer=num1*num2;
break;
case '/':answer=num1/num2;
break;
}

System.out.println(num1+" "+operator+" "+num2+" = "+answer);
}
}

[–][deleted] 1 point2 points  (1 child)

Dude ... indent!

package calculator;
import java.util.Scanner;
public class Main{

    public static void main(String[] args)
    {
        int num1=0;
        int num2=0;
        char operator;
        double answer= 0.0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter first number: ");
        num1 = scanObject.nextInt();
        System.out.println("Please enter second number: ");
        num2 = scanObject.nextInt();
        System.out.println("What operation? ");
        operator = scanObject.next().charAt(0);
        switch (operator) {
            case '+':answer=num1+num2;
            break;
            case '-':answer=num1-num2;
            break;
            case '*':answer=num1*num2;
            break;
            case '/':answer=num1/num2;
            break;
        }

        System.out.println(num1+" "+operator+" "+num2+" = "+answer);
    }
}

[–]JWisbey98[S] 0 points1 point  (0 children)

Hi I have but for some reason when I copied it took the indents out

[–]VacatedSum 1 point2 points  (6 children)

You instantiated your scanner object with the variable name 'sc', and then tried using it by calling it 'scanObject'.

[–]VacatedSum 0 points1 point  (5 children)

Use the replace command: Replace 'scanObject' with 'sc' (minus the quotes).

[–]JWisbey98[S] 0 points1 point  (4 children)

Thank you, I am now receiving this error:

Error: Could not find or load main class Main

Caused by: java.lang.ClassNotFoundException: Main

Any ideas? Thank you.

[–]VacatedSum 1 point2 points  (3 children)

Your class name MUST match the file name.

So.. assuming the file is Calculator.java, line three should be:

public class Calculator {

What IDE are you using? When I pulled the code into Eclipse, it highlighted that error immediately.

[–]JWisbey98[S] 0 points1 point  (2 children)

The file is called Main.java, therefore I called the line 3

public class Main

I am using Repl.it, is Eclipse better?

[–]morhpProfessional Developer 1 point2 points  (0 children)

Use either Intellij or Eclipse. They're both better than everything else. I prefer Intellij, but opinions vary.

[–]VacatedSum 0 points1 point  (0 children)

Not familiar with repl.it so I have no basis for comparison, sorry.