i'm really new to Java and i'm trying to make a basic calculator, I've managed to get it to work and produce a double as i desired. My problem is that i want it to provide an error message if user inputs the wrong data type e.g. String
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double n1, n2;
String n3;
Scanner inp = new Scanner(System.in);
System.out.print("Enter first value: ");
String inp1 = inp.nextLine();
n1 = Double.parseDouble(inp1);
System.out.print("Enter Second Value: ");
String inp2 = inp.nextLine();
n2 = Double.parseDouble(inp2);
System.out.print("Enter Operation: ");
String inp3 = inp.nextLine();
switch (inp3) {
case "+":
System.out.println("The result is: " + (n1 + n2));
break;
case "-":
System.out.println("The result is: " + (n1 - n2));
break;
case "*":
System.out.println("The result is: " + (n1 * n2));
break;
case "/":
System.out.println("The result is: " + (n1/n2));
break;
default:
System.out.println("Invalid Operation! \nPlease use '-,+,*,/' ");
}
}
}
Here's my code at the moment, im open to any constructive criticism to improve my code. I can't seem to figure out a way to solve my problem! Thanks for any help :)