How to solve the error in the switch / case? Java

0

It is requested to develop the algorithm that allows to analyze the behavior of the sales of the "La sweetzuela" ice cream shop.

For this, it is requested to enter a sample of 5 sales during a period of time.

You must enter:

a. Ice cream size:

i. c (Small glass)

ii. m (Medium glass)

iii. g (Cucurucho)

b. Quantity

c. Price

It must be calculated:

a. Total amount sold

b. Average amount per sale

c. Order of presentations based on sales (What size of ice cream is sold more, which less, and which is in the middle)

My problem is that when I enter vaso chico or vaso mediano , I get Error (the default value of the switch), but when I input cone this does not happen and I can not find the error.

int ventas = 6;
    String helados = " ";
    int cantidad1 = 0;
    int cantidad2 = 0;
    int cantidad3 = 0;
    int precio1 = 20;
    int precio2 = 30;
    int precio3 = 15;
    int total1 = 0;
    int total2 = 0;
    int total3 = 0;
    int totalVendido = 0;
    double promedioVenta = 0;
    int cantidad = 0;

    Scanner teclado = new Scanner(System.in);

    for (int i = 1; i < ventas; i++) { //El contador empieza desde el 1 hasta menor al valor de ventas

        System.out.println(i + ".Ingrese tamaño de helado (vaso chico / vaso mediano / cucurucho) : ");
        helados = teclado.next();


        switch (helados) {

        case "vaso chico":

            System.out.println(i + ".Ingrese Cantidad : ");
            cantidad = teclado.nextInt();

            total1 = precio1 * cantidad;

            cantidad3++;

            System.out.println("Total : " + total1);


            break;

        case "vaso mediano":

            System.out.println(i + ".Ingrese Cantidad : ");
            cantidad = teclado.nextInt();

            total2 = precio2 * cantidad;

            cantidad2++;

            System.out.println("Total : " + total2);


            break;

        case "cucurucho":

            System.out.println(i + ".Ingrese Cantidad : ");
            cantidad = teclado.nextInt();

            total3 = precio3 * cantidad;

            cantidad3++;

            System.out.println("Total : " + total3);


            break;

        default:

            System.out.println("Error!");

            break;

        }

    }

    totalVendido = cantidad1 + cantidad2 + cantidad3;

    System.out.println("Importe total vendido : " + totalVendido);

    promedioVenta = totalVendido / 5;

    System.out.println("Importe promedio por venta : " + promedioVenta);

    if (cantidad1 > cantidad2 && cantidad2 > cantidad3) {

        System.out.println("1°.Vaso Chico . \n 2°.Vaso Mediano . \n 3°.Cucurucho .");
    }

    else if (cantidad2 > cantidad3 && cantidad3 > cantidad1) {

        System.out.println("1°.Vaso Mediano . \n 2°.Cucurucho . \n 3°.Vaso Chico .");

    }

    if (cantidad3 > cantidad1 && cantidad1 > cantidad2) {

        System.out.println("1°.Cucurucho . \n 2°.Vaso Chico . \n 3°.Vaso Mediano .");

    }
    
asked by computer96 06.10.2018 в 01:01
source

2 answers

3

Change in:

helados = teclado.next();

By:

helados = teclado.nextLine();

When you use next you only use the first word before the space.

    
answered by 06.10.2018 / 01:19
source
1

Some tips to determine where the code is failing (without entering debugging tools):

1. Simplify the problem.

Surely you have some indication of where the program is failing: extract the part that you think is failing and see adding complexity until something fails. For example: you have a reading of a string by keyboard, which is passed to a switch , you can put that part in a test class and obviate the keyboard input by testing with the strings that can be expected:

package pruebas;

public class Zzz {

    public static void main(String[] args) {
        test("vaso chico");
        test("vaso mediano");
        test("cucurucho");
        test("xxx");
    }

    public static void test(String option) {
        switch (option) {

            case "vaso chico":
                System.out.println("Seleccionado vaso chico");
                break;

            case "vaso mediano":
                System.out.println("Seleccionado vaso mediano");
                break;

            case "cucurucho":
                System.out.println("cucurucho");
                break;

            default:
                System.out.println("Error !" + option);
                System.out.println("---> " + option); // HACK: trace
        } // switch
    }

} // class

2. Use traces.

In the previous code I added the line:

System.out.println("---> " + option); // HACK: trace

In case you enter the default condition you will know which string is being evaluated. If the string is not what you expected, check its origin.

3. Combine paths with completions.

You can add traces to determine if the program passes through certain points and end execution at different points.

// code
// ...
System.out.println(" line 12" ); // HACK: trace
// code
// ...
System.out.println(" line 17" ); // HACK: trace
// code
// ...
System.out.println(" line 23" ); // HACK: trace
System.exit(0);

4. Refactor to make the code easier to read and interpret.

Use methods to separate the different actions. Also, if you have repeated parts you can restructure to reduce the code easily, for example, notice that you are repeating the following in each case of the switch :

System.out.println(i + ".Ingrese Cantidad : ");
cantidad = teclado.nextInt();

Ok, do not you want to ask if they do not introduce a valid option? That's why you can not enter this part before the switch, but if you can use a method to ask the amount:

cantidad = getQuantity(teclado, i);

//...

private static int getQuantity(Scanner scn, int n) {
    System.out.println(n + ".Ingrese Cantidad : ");

    return scn.nextInt();
}

5. Use meaningful names for the variables.

This does not matter as much as you write your code today, but in 2 weeks you'll see ... I still do not know what the i does in the for , for that the method that I just put you is so clarifying.

6. Use try-catch blocks with generic exceptions.

When exceptions are thrown you have automatic tracings that help you determine what's wrong. Putting the "chunk" of code where the program fails within this structure usually helps.

For the method that returns the amount, which by the way I think will fail if you do not enter a number:

private static int getQuantity(Scanner scn, int n) {
    try {
        System.out.println(n + ".Ingrese Cantidad : ");
        return scn.nextInt();

    } catch(Exception e) {}

    return 0; // default
}
    
answered by 06.10.2018 в 02:06