Search code examples
javastringcomparisondo-while

how can I compare a string in a do-while loop from switch case


import java.util.Scanner;
public class WorkArea {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        
        String errorMessage="Invalid input entered. Terminating...";
        
        do {
        System.out.println("Operations: add subtract multiply");
        System.out.println("Enter an operation:");
        String operation=input.next();

        switch(operation.toLowerCase())
        {
        case "add":
            System.out.println("Enter two integers:");
            if(input.hasNextInt())
            {
                int int1=input.nextInt();
                    if(input.hasNextInt())
                    {
                        int int2=input.nextInt();
                        System.out.println("Answer:"+(int1+int2));
                    }else{
                        System.out.println(errorMessage);
                    }
            }else{
                System.out.println(errorMessage);
            }
            break;
        case "subtract":
            System.out.println("Enter two integers:");
            if(input.hasNextInt())
            {
                int int1=input.nextInt();
                    if(input.hasNextInt())
                    {
                        int int2=input.nextInt();
                        System.out.println("Answer:"+(int1-int2));
                    }else{
                        System.out.println(errorMessage);
                    }
            }else{
                    System.out.println(errorMessage);
                }
            break;
        case "multiply":
            System.out.println("Enter two integers:");
            if(input.hasNextDouble())
            {
                double double1=input.nextDouble();
                    if(input.hasNextDouble())
                    {
                        double double2=input.nextDouble();
                        System.out.printf("Answer:%.2f\n",double1*double2);
                    }else{
                        System.out.println(errorMessage);
                    }
            }else{
                    System.out.println(errorMessage);
                }
            break;
        
        default:
            System.out.println(errorMessage);
            break;
        }
    }while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
}
}

I have tried do-while loop to function the operations as I have listed in the snippet repeatedly by comparing the string from the switch case. I am expecting the program should terminate if the operation is out of the list.

while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));
while(!operation=="add"||"subtract"||"multiply");
while(!operation.equals("multiply"));

when I compare the string using do-while loop the result says "operation cannot be resolved". How can I perform multiple operations using do-while loop in a single console window


Solution

  • Variables defined inside a loop will have block scope, ie, they can only be used inside the loop body. In your code, the operation variable can only be used inside the loop body. Modifying your program -

    import java.util.Scanner;
    public class WorkArea {
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
    
            String errorMessage="Invalid input entered. Terminating...";
            String operation = null;
    
            do {
            System.out.println("Operations: add subtract multiply");
            System.out.println("Enter an operation:");
            operation=input.next();
    
            switch(operation.toLowerCase())
            {
                case "add":
                    System.out.println("Enter two integers:");
                    if(input.hasNextInt())
                    {
                        int int1=input.nextInt();
                        if(input.hasNextInt())
                        {
                            int int2=input.nextInt();
                            System.out.println("Answer:"+(int1+int2));
                        }else{
                            System.out.println(errorMessage);
                        }
                    }else{
                        System.out.println(errorMessage);
                    }
                    break;
                case "subtract":
                    System.out.println("Enter two integers:");
                    if(input.hasNextInt())
                    {
                        int int1=input.nextInt();
                        if(input.hasNextInt())
                        {
                            int int2=input.nextInt();
                            System.out.println("Answer:"+(int1-int2));
                        }else{
                            System.out.println(errorMessage);
                        }
                    }else{
                        System.out.println(errorMessage);
                    }
                    break;
                case "multiply":
                    System.out.println("Enter two integers:");
                    if(input.hasNextDouble())
                    {
                        double double1=input.nextDouble();
                        if(input.hasNextDouble())
                        {
                            double double2=input.nextDouble();
                            System.out.printf("Answer:%.2f\n",double1*double2);
                        }else{
                            System.out.println(errorMessage);
                        }
                    }else{
                        System.out.println(errorMessage);
                    }
                    break;
    
                default:
                    System.out.println(errorMessage);
                    break;
            }
        }while(!operation.equals("add")||!operation.equals("subtract")||!operation.equals("multiply"));