Reputation: 164
I recently posted a question about a scanner not giving the expected results and learned that my problem was that I wasn't flushing the scanner with .nextLine()
. I am confused with a program I'm working on because I am flushing the scanner properly but when I test my program, if - when prompted for a number - I type a string, I get erroneous output. It repeats the same loop twice.
The top of the loop has a call to nextLine() and the else block that deals with invalid input such as the string I'm typing has a call to nextLine() as well. But still somehow I'm getting bad output
So to be specific, here is a sample of the bad output with user input in bold and the problematic output in italics
Enter the left-hand value: 2
Enter operator: -
Enter the right-hand value: t
Invalid Input
Enter operator (+ - * or / :
Invalid Operator
Enter operator (+ - * or / :
The four lines above are automatically spit out to the console.
Here is the code snip, with a big comment where the bad code is. I would have posted just the while block where the issue is but since the while block is most of the program and the whole program is only a bit larger than that section, I figured it would be better to post all of it.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Calculator{
public static void main(String[] args){
double leftHandVal = 0.0;
//Output Title & Instructions
System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
System.out.print("!\t\t\t\t!\n");
System.out.print( "!\t INSTRUCTIONS\t\t!\n");
System.out.print("!\t\t\t\t!\n");
System.out.print("! INPUT\t\tOUTPUT\t\t!\n");
System.out.print("! *******\t *********\t\t!\n");
System.out.print("! c or C\t\tClear\t\t!\n");
System.out.print("! q or Q\t\tQuit\t\t!\n");
System.out.print("! +\t\tAddition\t\t!\n");
System.out.print("! -\t\tSubtraction\t!\n");
System.out.print("! *\t\tMultiplication\t!\n");
System.out.print("! /\t\tDivision\t\t!\n");
System.out.print("!\t\t\t\t!\n");
System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
while(true){
Scanner input = new Scanner(System.in);
char op = '\n';//(+, -, *, or /) will use in switch statement for their ascii decimal values
System.out.print("Enter the left-hand value: ");
//these blocks allow the code at the very bottom to not erroneously ask the user for extra input with hasNext() calls
if(input.hasNext("c") || input.hasNext("C")){//even though its unlikely for a user to clear so early...just in case
leftHandVal = 0.0;
}
else if(input.hasNext("q") || input.hasNext("Q")){//even though its unlikely for a user to quit so early...just in case
op = 113;//assign q for quit code
}
else if(input.hasNextDouble()){
leftHandVal = input.nextDouble();
/*
*
*BAD CODE INSIDE WHILE BELOW
*BAD CODE INSIDE WHILE BELOW
*BAD CODE INSIDE WHILE BELOW
*BAD CODE INSIDE WHILE BELOW
*
*/
while(true){
input.nextLine();
double rightHandVal = 0.0;
System.out.print("\nEnter operator (+ - * or / : ");
if(input.hasNext()){
op = input.next().charAt(0);
}
//if user wishes to cancel or quit on operator prompt, break out of inner while to access the clear and quit code
if(op == 99 || op == 67){
op = 99;
break;
}
else if(op == 113 || op == 81){
op = 113;
break;
}
else if((op != 43) && (op != 45) && (op != 42) && (op != 47)){//if invalid operator, restart inner while
System.out.print("Invalid Operator");
continue;
}
System.out.print("Enter the right-hand value: ");
if(input.hasNextDouble()){
rightHandVal = input.nextDouble();
switch(op){
case 43:
System.out.printf("%.3f + %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal + rightHandVal));
leftHandVal += rightHandVal;
break;
case 45:
System.out.printf("%.3f - %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal - rightHandVal));
leftHandVal -= rightHandVal;
break;
case 42:
System.out.printf("%.3f * %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal * rightHandVal));
leftHandVal *= rightHandVal;
break;
case 47:
System.out.printf("%.3f / %.3f = %.3f", leftHandVal, rightHandVal, (leftHandVal / rightHandVal));
leftHandVal /= rightHandVal;
break;
}
}
//if clear or quit requested from prompt for right-hand value, break to reach the clear and quit code
else if(input.hasNext("c") || input.hasNext("C")){
op = 99;
break;
}
else if(input.hasNext("q") || input.hasNext("Q")){
op = 113;
break;
}
else{
System.out.print("Invalid Input");
}
}
}
//if c || C reset op to null and restart outer while
if(op == 99 || op == 67){
op = '\n';
leftHandVal = 0.0;
continue;
}
//else if q || Q, prompt user with a popup to confirm.
if(op == 113 || op == 81){
int response = JOptionPane.showConfirmDialog(null, "QUIT CALCULATOR?", null, JOptionPane.YES_NO_OPTION);
if(response == 0){
System.exit(0);
}
continue;
}
}
}
}
Upvotes: 0
Views: 312
Reputation: 8653
else if(input.hasNext("c") || input.hasNext("C"))
{
op = 99;
break;
}
else if(input.hasNext("q") || input.hasNext("Q")){
op = 113;
break;
}
else{
System.out.print("Invalid Input");
}
in this code, when you enter "t" for right hand value, you just check for hasNext();
finally it comes to else and prints Invalid Input.
But input still has value "t", so it goes to second while loop starting again and
System.out.print("\nEnter operator (+ - * or / : ");
if(input.hasNext()){
op = input.next().charAt(0);
}
checks for input.hasNext() which already has "t", so takes "t" and continues.
Solution is to flush the "t" before coming to the while loop.
Upvotes: 1