Reputation: 71
This is my code in Java
import java.util.Scanner;
class calc
{
public static void main(String[] args)
{
boolean go=true;
Scanner input=new Scanner(System.in);
while(go)
{
int num1;
int num2;
int total;
int choice;
System.out.println("\nHi This is Console type Calculator");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multiply");
System.out.println("4. Divisoin");
System.out.print("Enter Your Choice : ");
choice=input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1+num2;
System.out.println("Addition of "+num1+" and "+num2+" are "+total);
break;
case 2:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1-num2;
System.out.println("Substraction of "+num1+" and "+num2+" are "+total);
break;
case 3:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1*num2;
System.out.println("Multiplication of "+num1+" and "+num2+" are "+total);
break;
case 4:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1/num2;
System.out.println("Divistion of "+num1+" and "+num2+" are "+total);
break;
default:
System.out.println("Please Choose right option...Try again");
break;
}
System.out.println("Do You Want more Calculation...Yes/No");
String str=input.nextLine();
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
if("no".equals(str1))
{
go=false;
System.out.println("Thanks For using...Bye");
}
}
}
}
And I have problem in the following portion of code as taking input. This is not taking any input from user skip this part. Is there any problem in this code.
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
if("no".equals(str1))
{
go=false;
System.out.println("Thanks For using...Bye");
}
Upvotes: 1
Views: 694
Reputation: 15842
The problem is because of the fact that you pick nextInt
earlier. The user inputs a number and a new line character. You pick the number and the new line character is kept buffered. When you perform nextLine()
it reads all characters between what is being pointed and the next EOL
. It reads empty String as it's in the buffer before \n
and then another nextLine()
requires the program to wait for the input.
When you put a breakpoint after String str = input.nextLine()
you'll see that it is actually empty String
: ""
.
So instead of:
System.out.println("Do You Want more Calculation...Yes/No");
String str=input.nextLine();
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
You should write:
input.nextLine();
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
Upvotes: 3
Reputation: 3029
Your problem is you were using nextInt()
in your cases, so your string stored is the new line. All you have to do is replace:
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
if("no".equals(str1))
{
go=false;
System.out.println("Thanks For using...Bye");
}
by:
System.out.println("Do You Want more Calculation...Yes/No");
input.nextLine(); // this is what I added
String str1=input.nextLine();
if("no".equals(str1))
{
go=false;
System.out.println("Thanks For using...Bye");
}
You can remove the following as those 2 lines serve no useful purpose:
System.out.println("Do You Want more Calculation...Yes/No");
String str=input.nextLine();
Explanation: the input.nextLine();
that I added basically captures the new line character when you hit enter or return. Next, you will have to call input.nextLine()
again and assign it to str1
to capture the string you intended to use.
Also, a small note on your program: if a user were to type in No, your program still considers the value as Yes, because it will change go to false only if no is lower case. A solution to that would be to convert the string you receive to lower case by calling str1.toLowerCase()
. Also, you might want to check for an explicit yes answer, because a user might type dog and your program still considers it as a yes.
Upvotes: -1