computer303030
computer303030

Reputation: 11

Not getting the expected output from the do-while loop

In this calculator program when I type in any other incorrect answer for the operator such as a number or a letter instead of +, -, *, / it shows the "wrong only operators" message but even when I put in the correct operator the same message still shows up. How can the program not show the wrong message when I type in the correct symbol.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) 
    {
        double num1, num2;
        double output = 0;
        char operator;  
        Scanner scan = new Scanner (System.in);
        
        System.out.println("Type in first number ");
        
        while(scan.hasNextDouble() == false) 
        {
            System.out.println("Wrong only numbers. ");
            scan.nextLine();
        }
        
        num1 = scan.nextDouble();
        
        System.out.println("Type in the operator ");
        
        do
        {
        operator = scan.next().charAt(0);
        System.out.println("Wrong only operators. ");
        scan.nextLine();        
        }
        
        while(operator != '+' && operator != '-' && operator != '*' && operator != '/');                        
        
        System.out.println("Type in second number ");
        
        while(scan.hasNextDouble() == false)
            {
            System.out.println("Wrong only numbers. ");
            scan.nextLine();
            }
        num2 = scan.nextDouble();
        
        switch (operator)
        {
            case '+': output = num1 + num2; break;
            case '-': output = num1 - num2; break;
            case '*': output = num1 * num2; break;
            case '/': output = num1 / num2; break;
            
        }       
        
        System.out.println("" + num1 + " " + operator + " " + num2 + " = " + output);
        
    }

}

Upvotes: 1

Views: 76

Answers (1)

Lovesh Dongre
Lovesh Dongre

Reputation: 1344

In your case it is better to use a while loop instead of a do while.

Since you are using a do while loop : that statement is being executed at least once, not matter whether the operator is correct or not.

You can add a condition there to stop it from executing but a better way is to use while loop

import java.util.Scanner;

class Main {
    public static void main(String[] args) 
    {
        double num1, num2;
        double output = 0;
        char operator;  
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Type in first number ");
        
        while(scan.hasNextDouble() == false) 
        {
            System.out.println("Wrong only numbers. ");
            scan.nextLine();
        }
        
        num1 = scan.nextDouble();
        
        System.out.println("Type in the operator ");
        
        operator = scan.next().charAt(0);
        
        while(operator != '+' && operator != '-' && operator != '*' && operator != '/')
        {

        System.out.println("Wrong only operators. ");
        operator = scan.next().charAt(0);
        scan.nextLine();        
        }
        
        System.out.println("Type in second number ");
        
        while(scan.hasNextDouble() == false)
            {
            System.out.println("Wrong only numbers. ");
            scan.nextLine();
            }
        num2 = scan.nextDouble();
        
        switch (operator)
        {
            case '+': output = num1 + num2; break;
            case '-': output = num1 - num2; break;
            case '*': output = num1 * num2; break;
            case '/': output = num1 / num2; break;
            
        }       
        
        System.out.println("" + num1 + " " + operator + " " + num2 + " = " + output);
        
}
}

Upvotes: 3

Related Questions