javaswitch-statementbreak

Switch executes default even with break in every case


So i'm trying to break out of the switch and while statements using the break in every case, yet even using the break statement my program keeps executing the default after the specific case code is executed, i'v tried for hours now yet i can't find a way arround it what should i do?

The code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //Creates scanner for user input
        Scanner input = new Scanner(System.in);

        //Creates variables to define the size of the array and the value to make the operations
        int arraySize, arrayCalc;

        //Gets the size of the array
        System.out.println("Please enter the size of your array:");
        arraySize = input.nextInt();
        input.nextLine();

        //Repeats if array size is zero
        while (arraySize <= 0) {
            System.out.println("The size of your array is too small, please set a bigger value:");
            arraySize = input.nextInt();
            input.nextLine();
        }

        //Gets the values of the array
        int[] array = new int[arraySize];
        System.out.println("Please enter the values for your array:");
        for (int i = 0; i < arraySize; i++) {
            array[i] = input.nextInt();
        }

        input.nextLine();

        System.out.println("Please, put the char compatible with the operation you want to do:");

        //Loop in case operator is not valid
        boolean validOperator = false;
        while(!validOperator) {

            //Sets the operator
            String operation = input.nextLine();

            switch (operation) {

                case "+":

                    //Addition operation
                    System.out.println("Please enter what value you would like to add to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] + arrayCalc;
                    }

                    break;

                case "-":

                    //Subtraction operation
                    System.out.println("Please enter what value you would like to subtract to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] - arrayCalc;
                    }
                    break;

                case "*":

                    //Multiplication operation
                    System.out.println("Please enter what value you would like to multiply to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] * arrayCalc;
                    }
                    break;

                case "/":

                    //Division operation
                    System.out.println("Please enter what value you would like to divide to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] / arrayCalc;
                    }
                    break;

                default:

                    //Exception if input is not a proper operator
                    System.out.println("That's not a valid operator! try \"+\" \"-\" \"*\" or \"/\".");

            }
        }

        //Prints the operation
        System.out.print("Those are your numbers after the operation: ");
        for (int i = 0; i < arraySize- 1; i++) {
            System.out.print(array[i] + ", ");
            if (i == arraySize - 2) {
                System.out.println(array[i + 1] + ".");
            }
        }
    }
}

Solution

  • Put validOperator = true; before every break statement in case block.