0

Im a beginner in JAVA and I have been trying to create a simple calculator program. I am facing a problem in the while loop where the loop does not wait for my input "ans" and directly jumps to the else clause

import java.util.Scanner;

public class scanner_program {

    public static void main(String[] args)
    {
        
        Scanner ob = new Scanner(System.in);
        int i = 1;
        while (i==1) 
        {   
            System.out.print("Enter a number: ");
            int num = ob.nextInt();
            System.out.print("Enter another number: ");
            int num2 = ob.nextInt();
        
            System.out.println("Sum of "+num+" And "+num2+" is "+(num+num2));
            System.out.println("Remainder of "+num+" And "+num2+" is "+(num-num2));
            System.out.println("Product of "+num+" And "+num2+" is "+(num*num2));
            float answer= (float) num/num2;
            System.out.println("Quotient of "+num+" And "+num2+" is "+answer);
        
            System.out.print("Try Again? (Y/N) ");
            String ans = ob.nextLine();
        
            if (ans=="Y" || ans=="y" ) {
                // do nothing
            }
            else if (ans=="N" || ans=="n"){
                break;
            }
            else {
                System.out.println("Invalid input");
                break;
                }
            }   
    
        }
}
2
  • does your code working you are not updating i value Commented Jul 21, 2020 at 17:31
  • Does this answer your question? Using scanner.nextLine() Also, when naming your classes in Java, you should be CamelCase, so ScannerProgram Commented Jul 21, 2020 at 17:39

2 Answers 2

1

ob.nextLine() is taking the newline character as its next value, so ans is set to an invalid character.

change ob.nextLine() to ob.next().

Also, for Java, you should use string.equals("another string") so change your letter checking to this:

if (ans.equals("Y") || ans.equals("y") ) {
  // do nothing
}
else if (ans.equals("N") || ans.equals("n")){
  break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or you could write: if (ans.equalsIgnoreCase("y"))
0
public static void main(String[] args){
    Scanner ob = new Scanner(System.in);
    while(true){
        System.out.print("Enter a number: ");
        int num = ob.nextInt();
        System.out.print("Enter another number: ");
        int num2 = ob.nextInt();
    
        System.out.println("Sum of "+num+" And "+num2+" is "+(num+num2));
        System.out.println("Remainder of "+num+" And "+num2+" is "+(num-num2));
        System.out.println("Product of "+num+" And "+num2+" is "+(num*num2));
        float answer= (float) num/num2;
        System.out.println("Quotient of "+num+" And "+num2+" is "+answer);
    
        System.out.print("Try Again? (Y/N)");
        ob.nextLine();
        String ans = ob.nextLine();
    
        if (ans.equalsIgnoreCase("y")) {
            // do nothing
        } else if (ans.equalsIgnoreCase("n")){
            break;
        } else {
            System.out.println("Invalid input");
            break;
        }
    } 
}

You should enter ob.nextLine() to clear your input for other inputs you may enter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.