I am fairly new to Java, never have done programming before. I am getting the following errors when trying to run the code for each of the operators. I am wanting it to take in the user input as a string but return a double as the result. I know there will probably be several errors, but any help is appreciated.
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from String to double The operator - is undefined for the argument type(s) java.lang.String, java.lang.String The operator * is undefined for the argument type(s) java.lang.String, java.lang.String The operator / is undefined for the argument type(s) java.lang.String, java.lang.String The operator % is undefined for the argument type(s) java.lang.String, java.lang.String Cannot make a static reference to the non-static field Math Cannot make a static reference to the non-static field Math command cannot be resolved to a type The operator >= is undefined for the argument type(s) Class, int Syntax error on token ".", class expected after this token The operator <= is undefined for the argument type(s) String, int
at Calculator.main(Calculator.java:72)
import java.util.Scanner;
import java.lang.Math;
/*@public class for calculator*/
public class Calculator {
String ADD = "+";
String SUB = "-";
String DIV = "/";
String MUL = "*";
String MOD = "%";
String SQRT = "sqrt";
String POW = "^";
String Math.PI = "pi";
public static void main(String[] args) {
/*@Scanner to read user input */
Scanner myScanner = new Scanner(System.in);
boolean continueLoop = true;
/*@try statement to begin program */
try {
String x,y,command;
double result = 0;
double myDouble = result;
/*@print line welcoming user to calculator */
System.out.print("Welcome to my Calculator! Lets Begin!" + "\n");
/*@print line asking user to either type HELP or START to begin */
System.out.print("Type HELP or START to begin: ");
/*@string to read user input for HELP or START command */
String firstCommand = myScanner.nextLine();
if (firstCommand.equals("HELP")) {
System.out.println("Welcome to my calculator. Make sure to enter only numbers into the first two fields. \nDo NOT divide by 0. \nBe sure to enter the number matching your desired operation.");
System.out.print("Type HELP or START to begin: ");
command = myScanner.nextLine();
}
Double firstNumber = Double.parseDouble(myScanner.nextLine());
/*@print statement asking user for first number input */
System.out.println("Please Enter your First number: ");
/*@variable x to be read by scanner */
x = myScanner.nextLine();
Double secondNumber = Double.parseDouble(myScanner.nextLine());
/*@print statement asking user for second number input *
System.out.println("Please Enter your Second Number: ");
/*@variable y to be read by scanner */
y = myScanner.nextLine();
/*@param converts string into double*/
Double converted = Double.parseDouble(myScanner.nextLine());
/*Print statements with instructions on what commands to put in and where to put in.*/
System.out.print("\n1: Add. \n2: Sub. \n3: Mult. \n4: Divide. \n5: Modulo. \n6: Pi \n7: Pow.");
System.out.print("\nEnter your operator: ");
command = myScanner.nextLine();
switch(command) {
/*@returns addition of firstNumber and secondNumber */
case "1":
result = (x+y); break;
/*@returns subtraction of firstNumber and secondNumber */
case "2":
result = (x-y); break;
/*@returns multiplication of firstNumber and secondNumber */
case "3":
result = (x*y); break;
/*@returns division of firstNumber and secondNumber */
case "4":
result = (x/y); break;
/*@returns modulo of firstNumber and secondNumber */
case "5":
result = (x%y); break;
/*@returns Math.PI */
case "6":
result = (Math.PI);
/*@returns power of firstNumber to the second number secondNumber*/
case "7":
result = (Math.pow(x, y));
}
if(command. >= 1 && command <= 5)
System.out.println("Your answer is: " + result);
/*@if statements for all mathematical operators */
// cannot get sqrt, pow, pie to work
/*@throws exception if anything other than a number is typed in by user */
/*catch exception if anything other than number is keyed in*/
} catch (InputMismatchException inputMismatchException) {
/*@error statement printed*/
System.err.printf("%nException: %s%n", inputMismatchException);
/*@statement for scanner to read the next line*/
myScanner.nextLine(); // discard input, so user can try again
/*@print statement letting user know to try again*/
System.out.printf("You must enter integers. Please try again %n%n");
}
/*@throws exception if zero is keyed in as denominator
/*exception if zero is keyed in as the denominator in division since you cannot divide by zero*/
catch (ArithmeticException arithmeticException) {
/*error statement printed*/
System.err.printf("%nException: %s%n", arithmeticException);
/*print statement letting user that 0 is not valid and to please try again
not able to get the loop to go back and restart*/
System.out.printf("Zero is an invalid denominator.Please try again. %n %n");
}
/*@param finally statement closes scanner*/
finally {
myScanner.close();
} while(continueLoop);
}
}
Below are few issues that I can see at first sight.
myScanner.toString() doesn't read the user input. To read user input you might use myScanner.next().
Inside if condition if (command.equals("HELP"))...
, here you haven't read the command from user input, It seems you wanted to use if (firstCommand.equals("HELP"))
.
5.Variable named "firstNumber" and "secondNumber" never used in the code.
myScanner.nextLine()
before prompting the user to provide input.case "1": // The datatype of command is String so it should be inside quotes "".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.