与 try/catch InputMismatch 异常作斗争并从数组中检索数据

Struggling with try/catch InputMismatch Exception and retrieving data from an array

我正在尝试执行以下操作:


package latest;
import java.util.Scanner; 
import java.util.InputMismatchException;
import java.util.Random;

public class Latest 
{
    private static int[] randomInteger;

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int indexPosition = 0;    
        randomInteger = new int[100];
        Random rand = new Random();
        for (int i=0; i<randomInteger.length;i++)           
            randomInteger[i] = rand.nextInt();

        while (indexPosition < 0 || indexPosition > 100) 
        {         
            try
            {
                // get array index position
                System.out.println ("Hello, please enter an integer for the array index position: ");
                indexPosition = input.nextInt();
            }
            catch (InputMismatchException e)
            {
                System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100");
                indexPosition = input.nextInt();
            }
            {    
                System.out.println ("You did not input a valid value. Please enter an Integer value between 0 and 100");
                indexPosition = input.nextInt();
                System.out.println (randomInteger[indexPosition]);
            }
        }
    }
}

我的问题是代码编译但不输出任何内容,IDE 说 indexPosition - "Assigned value never used"

编辑:如果您输入有效的整数,评论中的 ingrid 代码可以完美运行,但它不会捕获任何异常,它只会崩溃

你快到了。试试下面的版本。

package latest;

import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;

public class Latest {

private static int[] randomInteger;

public static void main(String[] args) {
    int indexPosition = 0;
    Scanner input = new Scanner(System.in);
    randomInteger = new int[100];
    Random rand = new Random();
    for (int i = 0; i < randomInteger.length; i++)

        randomInteger[i] = rand.nextInt();

    System.out
            .println("Hello, please enter an integer for the array index position: ");
    do {
        try {
            // get array index position
            indexPosition = input.nextInt();
        } catch (InputMismatchException e) {
            System.out
                    .println("You did not input a valid value. Please enter an Integer value between 0 and 100");
            input.nextLine();
            continue;
        } catch (Exception e) {
            System.out
                    .println("You did not input a valid value. Please enter an Integer value between 0 and 100");
            input.nextLine();
            continue;
        }
        if (indexPosition < 0 || indexPosition > 100) {
            System.out
                    .println("You did not input a valid value. Please enter an Integer value between 0 and 100");
        }

    } while (indexPosition < 0 || indexPosition > 100);
    System.out.println(randomInteger[indexPosition]);
    input.close();
}
}