简体   繁体   中英

Java try-catch statement inside while loop

I have this method and it works fine. I need to put a try/catch statement so the method can continue if the user puts in a letter. I don't know where to put the statement, It seems everywhere I put it it get's wrong. Could somebody please show me where to put this statement?

public void myMethod() {

    Scanner in = new Scanner(System.in);
    int array[] = new int[21];
    int number;

    boolean end = false;

    while (!end) {

        System.out.println("Please give an number between 0-20: ");
        number = in.nextInt();

        for (int i = 1; i < array.length; i++) {

            if (i == number) {

                System.out.println(array[number]);
                end = true;
            }
        }
        if (!end) {
            System.out.println("I cant find number " + number
                    + " in the array, please try again ");
        }

    }

 }
    System.out.println("Please give an number between 0-20: ");
    try{
        number = in.nextInt();
    }catch(Exception e){
        number = 1; //Put random number of default number here
    }
public static void main(String[] args) {

    Test test = new Test();
    Scanner in = new Scanner(System.in);
    int array[] = new int[21];  
    int number;
    System.out.println("Please give an number between 0-20: ");
    do{
        try{
            number = Integer.parseInt(in.next());
        }
        catch(Exception e){
            System.out.println("Please give an number between 0-20: ");
            number = -1;
        }
    }
    while(!(number <= 20 && number >=0 ));
    System.out.println(array[number]);
}

Your for loop I can't explain, you need only check values between 0 and 20, And when you call try catch, you have to skip loop after exception

public static void myMethod() {

    Scanner in = new Scanner(System.in);
    int array[] = new int[21];
    int number=0;

    boolean end = false;

    while (!end) {


        System.out.println("Please give an number between 0-20: ");
        //check symbol
        try{
            number = Integer.valueOf(in.next());
        }catch(Exception e)
        {
            System.out.println("It's not a number! ");
            continue; //skip loop
        }

        if((number>=0)&&(number<=20))
        {
              System.out.println(array[number]);
              end=true;
        }
        else
             System.out.println("I cant find number " + number
                        + " in the array, please try again ");

        /* why do you use loop here???
         * u need to check if number between 0-20
        for (int i = 1; i < array.length; i++) {

            if (i == number) {

                System.out.println(array[number]);
                end = true;
            }
        }*/


    }

 }

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