1
I have an exercise to solve, which consists of creating a simple Java language calculator.
I have to send the two numbers through Terminal as arguments to my program. I am using the following build command:
zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src/socp1 $ javac MainEX1.java
Only it gives me the following mistake:
EX1.java
MainEX1.java:62: error: cannot find symbol
CalculatorMethodos cM = new CalculatorMethodos();
^
symbol: class CalculatorMethodos
location: class MainEX1
MainEX1.java:62: error: cannot find symbol
CalculatorMethodos cM = new CalculatorMethodos();
^
symbol: class CalculatorMethodos
location: class MainEX1
2 errors
I leave down the MAIN class:
int num1, num2, total = 0, opcCalc, vef = -1;
CalculatorMethodos cM = new CalculatorMethodos();
//read from keyboard
Scanner lerDataKeyBoard = new Scanner(System.in);
// BufferedReader lerDataKeyBoard = new BufferedReader(new InputStreamReader(System.in));
/* System.out.println("First number:\n");
num1 = lerDataKeyBoard.nextInt();
System.out.println("Second number:\n");
num2 = lerDataKeyBoard.nextInt(); */
System.out.println("Introduza a operação:\n");
System.out.println("'1' - SUM\n");
System.out.println("'2' - SUBTRACT\n");
System.out.println("'3' - MULTIPLY\n");
System.out.println("'4' - DIVIDE\n");
opcCalc = lerDataKeyBoard.nextInt();
switch (opcCalc) {
case 1:
total = cM.add(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
break;
case 2:
// total = cM.sub(args[0], args[1]);
break;
case 3:
// total = cM.sub(args[0], args[1]);
break;
case 4:
// total = cM.div(args[0], args[1]);
break;
}
System.out.println("Resultado:" + total);
}
CALCULATOR CLASS:
public class CalculatorMethodos {
private int total;
public int add (int num1, int num2) {
return total = num1 + num2;
}
public int sub(int num1, int num2) {
return total = num1 - num2;
}
public int mult (int num1, int num2) {
return total = num1 * num2;
}
public int div (int num1, int num2) {
return total = num1 / num2;
}
}
SOLUTION The solution to this error was to use the javac command with -cp and "..":
javac -cp .. MainEX1.java
NEW MISTAKE - when I try to use the command java MainEX1
Error: Could not find or load main class MainEX1
And the class code Calculatormethodos ?? Has not ?
– NoobSaibot
@Wéllingthonm..
– ZelDias
@Wéllingthonm.Souza can check
– ZelDias
You imported him into the class Main ?
– NoobSaibot
he’s inside the same package :)
– ZelDias
You copied the class code Calculatormethodos and glued here ? Because if that was still close
}
– NoobSaibot