1

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");
        }   
7
  • Can you tell what value do you get in str1? Commented Dec 27, 2016 at 12:44
  • Read about Yoda conditions Commented Dec 27, 2016 at 12:44
  • @GurwinderSingh It doesn't take any input in str1. Commented Dec 27, 2016 at 12:46
  • You might want to try using Scanner.next, that will search for the next complete token. Commented Dec 27, 2016 at 13:05
  • use if(str1.equalsIgnoreCase("no")){} in place of if("no".equals(str1)) Commented Dec 27, 2016 at 13:16

2 Answers 2

3

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: "".

enter image description here

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();
Sign up to request clarification or add additional context in comments.

3 Comments

That's a basic mistake with the scanner. Should be explain in every good tutorial...
Why wouldn't you use, input.next()? I think they are looking for the next complete token, not the next line.
@matt maybe... It's not the case
-1

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.

3 Comments

What about just if(str1.equalsIgnoreCase("no")){}?
@YoungMillie works equally well without any particular advantages though. Same computational cost. But +1 for more suggestions.
@YoungMillie Not really. toLowerCase will still convert every ASCII character to lowerCase while equalsIgnoreCase will exactly do the same number of comparisons even at the compiler level. --> Compiler Design course completed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.