I want the user to input a math equation using one of these variables: *, /, +, -
Input Examples (with no spaces) : 2*2, 35.7/3, 4.5+5.5
Then I want to break the first number, char variable, and second number apart and store them into separate variables.
When I use this code:
import java.io.*;
import java.util.Scanner;
class Extra {
public static void main (String[] args) throws IOException {
double fnum, snum;
char operator;
System.out.println("Type Operation.");
Scanner s = new Scanner ( System.in );
fnum = s.nextDouble();
operator = s.next().charAt(fnum + 1);
snum = s.nextDouble();
System.out.println(fnum);
System.out.println(snum);
System.out.println(operator);
s.close();
}
}
I get this error/response:
Type Operation.
2*2
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Extra.main(Extra.java:24)
I really want to use something like this:
operator = s.next("[*,/,+,-]").charAt(fnum + 1);
Please Help, I don't know what I'm doing wrong.