Reputation: 23
Am trying to create a basic calculator (new to Java). at the moment the user has to enter a number, followed by an operator and then a number. how can i get all three inputs on a single line?
import java.util.Scanner;
import java.io.*;
public class Calculator
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
double numb1, numb2, add, subtract, multiply, divide;
char operation;
while (true)
{
System.out.println("Calculate: ");
numb1 = in.nextDouble();
operation = in.next().charAt(0);
numb2 = in.nextDouble();
switch(operation)
{
case '+':
add = numb1 + numb2;
System.out.println(add);
break;
case '-':
subtract = numb1 - numb2;
System.out.println(subtract);
break;
case '*':
multiply = numb1 * numb2;
System.out.println(multiply);
break;
case '/':
divide = numb1 / numb2;
System.out.println(divide);
break;
}
}
}
}
Upvotes: 2
Views: 7998
Reputation: 682
Oh, the calculator - a true classic!
You can ask the Scanner
class to give you the whole sentence, instead of just the next number by calling nextDouble()
.
You're already doing this in the expression in.next().charAt(0)
.
However the problem that arises from this is that now you have a whole sentence of text, which is actually a mathematical term. How to separate this sentence into a structure that reflects the mathematical term is the work of something called Parser.
That is a non-trivial thing to do in general, but depending on the task you need to do, you'll probably don't need a full blown parser here. If you stick to your example of a single operation on two numbers, you might use a regular expression to extract the information. For non-negative numbers without decimals, you might use this:
Pattern termPattern = Pattern.compile("\\s*(\\d*)\\s*([+-*/])\\s*(\\d*)\\s*");
final String sentence = in.next();
Matcher matcher = termPattern.matcher(sentence );
if (!matcher.matches()) {
throw new IllegalArgumentException("Not a supported mathematical expression: " + sentence);
}
Integer firstNum = Integer.valueOf(matcher.group(1));
Integer secondNum = Integer.valueOf(matcher.group(3));
String operation = matcher.group(2);
switch (operation) {
...
}
I've used Regular Expressions to extract the information from the String
using the Java Pattern Class.
However while this is a common thing to do, please note that Regular Expression can't replace a Parser in all cases (and sadly it's tried far too often).
Depending on what you need to do with you Calculator, it might fit the bill.
This solution is still far from beeing perfect, but it supports a range of inputs and is adjustable to some degree. You can easily add negative or decimal values. However if you try to add more complex expressions with (
and )
you will reach it's limits. Good luck on exploring!
Upvotes: 1
Reputation: 34146
You can try using nextLine()
(returns a String
), then split it by spaces, and finally convert them to the corresponding types:
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
double numb1, numb2;
char operation;
// Split string by space
String[] parts = scanner.nextLine().split(" ");
// Convert to corresponding types
numb1 = Integer.parseInt(parts[0]);
operation = parts[1].charAt(0);
numb2 = Integer.parseInt(parts[2]);
...
}
So the input will need to be as:
1 + 2
with spaces between operators
Upvotes: 1