dimanche 21 mars 2021

Print statement inside if statement not executing, even though the rest of the if statement is

I'm having trouble figuring out why the code below won't print the word "banana" when I enter an invalid operator. I can tell the boolean errorFree still gets set to false because the prints in the switch statement below are never executed, but with an input like "3", "z", "5". I just get a blank output instead of the word I'm looking for. Thanks for taking the time to look at this!

 import java.util.Scanner;
    
    public class Lab9e {
        
        public static String getInput(String[] x) {
            return x[0] + " " + x[1] + " " + x[2];
        }
        
        public static boolean inArray(String s, String[] list) {
            boolean isPresent = false;
            for (int i = 0; i < list.length; i++) {
                if (s == list[i])
                    isPresent = true;
            }
            return isPresent;
        }
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            final String[] operators = {"+" , "-", "*", "/"};
            String[] userInputs = new String[3];
            System.out.print("Please enter an integer:");
            userInputs[0] = sc.next();
            System.out.print("Please enter the operation you would like to be performed:");
            userInputs[1] = sc.next();
            System.out.print("Please enter an integer:");
            userInputs[2] = sc.next();
            System.out.println("Input " + getInput(userInputs));
            sc.close();
            System.out.print("Output: ");
            boolean errorFree = true;
            int int1 = 0, int2 = 0;
            try {
                int1 = Integer.parseInt(userInputs[0]);
            }
            catch (NumberFormatException ex) {
                System.out.println("first input is not an integer");
                errorFree = false;
            }
            try {
                int2 = Integer.parseInt(userInputs[2]);
            }
            catch (NumberFormatException ex){
                System.out.println("third input is not an integer");
                errorFree = false;
            }
            if (inArray(userInputs[1], operators)){
                System.out.println("banana");
                errorFree = false;      
            }
            
            if (errorFree == true) {
                switch (userInputs[1]) {
                    case "+": 
                        System.out.println(int1 + int2);
                        break;
                    case "-": 
                        System.out.println(int1 - int2);
                        break;
                    case "*": 
                        System.out.println(int1 * int2);
                        break;
                    case "/": 
                        try{
                            System.out.println(int1 / int2);
                        }
                        catch (ArithmeticException ex){
                            System.out.println("Cant divide by zero");
                        }
        
                }
            }
        }
    
    }

Aucun commentaire:

Enregistrer un commentaire