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;
                }
            }   
    
        }
}
Natsuki
  • 23
  • 4
  • does your code working you are not updating `i` value – deadshot Jul 21 '20 at 17:31
  • Does this answer your question? [Using scanner.nextLine()](https://stackoverflow.com/questions/5032356/using-scanner-nextline) Also, when naming your classes in Java, you should be CamelCase, so ScannerProgram – Suede Jul 21 '20 at 17:39

2 Answers2

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;
}
mostegg
  • 33
  • 7
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.