Reputation: 259
I am creating a java calculator, and I am encountering a situation involving my java scanner being unable to accept any more input.
Here is the code im working with:
import java.util.Scanner;
class RealCalc {
public static void main(String args[]) {
Scanner bucky = new Scanner(System.in);
Scanner reason = new Scanner(System.in);
Scanner finalProduct = new Scanner(System.in);
double fnum, snum, answer;
String YesNo;
String Multiply;
String Add;
String Divide;
String Subtract;
String operation;
// Time to declare the string's value
Multiply = ("Multiply");
Divide = ("Divide");
Subtract = ("Subtract");
Add = ("Add");
System.out.println("Hello, my name is Cortana,"
+ " would you like to perform operations with me?");
YesNo = reason.nextLine();
if (YesNo.equals("Yes")) {
System.out.println("Good choice!");
System.out.println(" ");
System.out.println("Enter your first number");
fnum = bucky.nextDouble();
System.out.println("Enter your second number");
snum = bucky.nextDouble();
System.out.println("What do you want me to do with these numbers?");
operation = finalProduct.nextLine();
if (operation.equals(Multiply)) {
System.out.println(fnum * snum);
} else {
System.out.println("I do not recognize this operation");
}
}
}
}
So basically the problem is near the end when I am trying to find a way to code a thing where it asks you to type an operation you want to perform.
If you say Multiply, Divide, Add, or Subtract, it will do that respected operation with firstNum and secondNum (fnum, snum), but if it is neither of those 4 operations, it will say "I do not recognize this operation.
Now this isn't difficult. The problem is getting to retry typing in an operation.
When it says "I do not recognize" the program im using (eclipse) does not allow me to input any more data.
Please help.
Upvotes: 1
Views: 770
Reputation:
Try this:
String answer="";
String option="";
boolean status=true;
while(status){
Scanner num=new Scanner(System.in);
Scanner lin=new Scanner(System.in);
Scanner opt=new Scanner(System.in);
double numA,numB,result=0;
System.out.println("\"WELCOME TO JAVA CALCULATOR\"");
System.out.println("Enter The First Number");
numA=num.nextDouble();
System.out.println("Enter The Second Number");
numB=num.nextDouble();
System.out.println("What You Want To Calculate?");
answer=lin.nextLine();
if(answer.equals("+")){
result=numA+numB;}
if(answer.equals("-")){
result=numA-numB;}
if(answer.equals("*")){
result=numA*numB;}
if(answer.equals("/")){
result=numA/numB;}
System.out.println("The Result Is:"+result);
System.out.println("Do You Want To Contenue?");
option=opt.nextLine();
if(option.equals("y")){
status=true;}
if(option.equals("n")){
System.exit(0);
Upvotes: 1
Reputation: 4434
Here is another simple way to do it: wrapp the code area with the issue in a while(true)
and use a break
to finish when it has been answered
while (true) {
System.out.println("What do you want me to do with these numbers?");
operation = finalProduct.nextLine();
if (operation.equals(Multiply)){
System.out.println(fnum * snum);
break;
}else{
System.out.println("I do not recognize this operation");
}
}
This way your application will keep looping every time the user inputs an incorrect operation and will finish only when he inputs Multiply
The construct like while(true)
is used to create an infinite loop, so all the code inside the while block will keep running unless it encounters a break, which will happen only when he inputs the right operator and after the program outputs the answers. Otherwise if he inputs something incorrect the program will answer I do not recognize this operation
and will start over again by asking him again to input the operator
Upvotes: 3
Reputation: 3739
A working calculator (that re-prompts the user if operation is not recognized):
import java.util.Scanner;
class RealCalc {
// Declare the constants
private static final String YES = "YES";
private static final String MULTIPLY = "Multiply";
private static final String DIVIDE = "Divide";
private static final String SUBTRACT = "Subtract";
private static final String ADD = "Add";
public static void main(String args[]) {
// Using one only Scanner
Scanner sc = new Scanner(System.in);
double fnum, snum;
System.out.println("Hello, my name is Cortana,"
+ " would you like to perform operations with me?");
// Use equalsIgnoreCase to make it case-insensitive
if(sc.nextLine().equalsIgnoreCase(YES)) {
System.out.println("Good choice!\n");
System.out.print("Enter your first number: ");
fnum = sc.nextDouble();
System.out.print("Enter your second number: ");
snum = sc.nextDouble();
// This is used to clear the '\n' character following the previous
// integer input
sc.nextLine();
// Call the function to perform arithmetic operations
performOperation(sc, fnum, snum);
}
System.out.println("Thank you!");
}
public static void performOperation(Scanner sc, double fnum, double snum) {
System.out.println("\n" + MULTIPLY + ", " + DIVIDE + ", " + SUBTRACT + ", or " + ADD);
System.out.println("What do you want me to do with these numbers?");
String operation = sc.nextLine();
System.out.println();
if(operation.equalsIgnoreCase(MULTIPLY)) {
System.out.printf("%8.3f * %8.3f = %8.3f%n%n", fnum, snum, fnum * snum);
}
else if(operation.equalsIgnoreCase(DIVIDE)) {
System.out.printf("%8.3f / %8.3f = %8.3f%n%n", fnum, snum, fnum / snum);
}
else if(operation.equalsIgnoreCase(SUBTRACT)) {
System.out.printf("%8.3f - %8.3f = %8.3f%n%n", fnum, snum, fnum - snum);
}
else if(operation.equalsIgnoreCase(ADD)) {
System.out.printf("%8.3f + %8.3f = %8.3f%n%n", fnum, snum, fnum + snum);
}
else {
System.out.println("Sorry, I do not recognize this operation.\n");
// Call this function again to re-prompt the user for a valid operation
performOperation(sc, fnum, snum);
}
}
}
Output (Division):
Hello, my name is Cortana, would you like to perform operations with me?
yes
Good choice!
Enter your first number: 10
Enter your second number: 20
Multiply, Divide, Subtract, or Add
What do you want me to do with these numbers?
no
Sorry, I do not recognize this operation.
Multiply, Divide, Subtract, or Add
What do you want me to do with these numbers?
haha
Sorry, I do not recognize this operation.
Multiply, Divide, Subtract, or Add
What do you want me to do with these numbers?
divide
10.000 / 20.000 = 0.500
Thank you!
Note:
static final
keywordsequalsIgnoreCase
to make the comparison case-insensitivesc.nextLine();
to clear the '\n'
character following the previous integer input before asking for the operation to be performedif - elseif - else
to go through the different optionsperformOperation
), call the function again if the 'operation is not recognized'Upvotes: 0
Reputation: 10590
Hint: Use while(continue == 'Y')
loop with statement at the end of it like "press Y if you want to continue or any key to terminate".
Where and what to put in while
depends what you want to achieve. If you want to receive all data new (fnum, snum, operation
), then while loop must start before line
System.out.println("Enter your first number");
if you want only operation to be re-entered by user then use while loop
System.out.println("Enter your first number");
This is the basic version. You can of course enchant it more.
Upvotes: 0