简体   繁体   中英

Do-while, try-catch loop error

Im trying to write code for a school project, the main objective is to get the average gpa of a students semester depending on how many Subjects and Units you input, however, if I try typing 0, the program goes into an infinite try-catch loop with "You can only type positive numbers" Im using valueOf() because I want the user to be able to type "salir" which means exit, to exit the program.

  Scanner LeerTeclado = new Scanner(System.in); 
    int n=0, i=0, suma=0, promedio=0;
    String materia, cadena;

    //---------------------------------------------------------------------------
    out.println("---------------------------");
    out.println("--    School Grades      --");
    out.println("---------------------------");

    //---------------------------------------------------------------------------
    out.println("\nType 'salir' to terminate the program");
    out.println("-----------------------------------------");
    out.print("Type the number of subjects to grade: ");
    cadena = LeerTeclado.nextLine();
    int z = 0;
    if("salir".equals(cadena)){
        System.exit(0);
    }
    if("Salir".equals(cadena)){
        System.exit(0);
    }
    ///////////////////////////////////////////////////////////////////
    do{

        try{

            z = Integer.valueOf(cadena);

            if(z <= 0){
                out.println("...............................................");
                out.println(" You can only type positive numbers            ");
                out.println("..............................................."); 
                out.println("\n");
                continue;
            }


            break;

            }catch(NumberFormatException ex){

        out.println("\n*You have entered non-numeric characters*");
        out.print("\nPlease type the number of subjects again: ");
        LeerTeclado.nextLine();

            }
    }while(true);

In the try block, before you write

continue;

but after "You can only type positive numbers," you should prompt the User for another line of input, and wait for the user to enter that.

The "continue" statement skips to the end of the loop and causes the 2nd part of the loop not to run. That is why the loop is running indefinitely.

Move reading cadena into the try block

int z = 0;
do {
    try {
        cadena = LeerTeclado.nextLine(); // <-- re-read
        if ("salir".equalsIgnoreCase(cadena)) { // <-- you might test once.
            System.exit(0);
        }
        // if ("Salir".equals(cadena)) {
        //  System.exit(0);
        // }
        z = Integer.valueOf(cadena); // <-- or this loops forever.

Alberto, There are a few things that need to be changed in order to get this program to work the way you wish. Since you are a student I'm not going to solve it for you. I will answer your question, however.

When you type zero on the command line your program will execute from the z<=0 test down to the continue statement. The continue statement tells the code to ignore everything after and return to the beginning of the loop so it goes back to the beginning of the do statement and repeats. You need some way to end the loop.

May I suggest writing the program a little at a time and test as you go along. That is, write the part that's not in the loop. Once that works write a little something in the loop and test. Keep doing this until the programs works the way you want it to.

Good Luck

  do{ try{ z = Integer.valueOf(cadena); if(z <= 0){ out.println("..............................................."); out.println(" You can only type positive numbers "); out.println("..............................................."); out.println("\\n"); continue; } 

You want to use break

 if(z <= 0){
            System.out.println("...............................................");
            System.out.println(" You can only type positive numbers            ");
            System.out.println("..............................................."); 
            System.out.println("\n");
            break;
        }

continue just hops to the top of the if and keeps at it, same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM