0

I'm trying to do the following:

  • Call a method that creates an Array with 100 randomly generated integers.
  • Call a method that prompts the user to enter an index position of the array and returns the value entered. If the value entered by the user is not a valid integer, throw an "Input Mismatch Exception" that prompts the user of the problem and permits them the opportunity to input a correct integer or exit the program.

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]);
            }
        }
    }
}

My problem is the code compiles but does not output anything and the IDE is saying indexPosition - "Assigned value never used"

edit: ingrid's code in the comments works perfectly if you enter a valid integer however it doesn't catch any exceptions it just crashes

archer
  • 89
  • 8
  • Please fix your indenting.. – 3kings Mar 29 '16 at 03:13
  • Check the conditions of your `while()` loop they seem to be funky because `indexPosition` is not `< 0 || > 100` at the start or ever in that case. It would need to be more like `while(indexPosition != -1)` (USER ENTERS -1 to exit the loop? – 3kings Mar 29 '16 at 03:20
  • I'm not sure if it should be `if`' instead of `while` because negative numbers are fine I just need it if the user enters `0.5` or a `string` for the program to catch it and to ask for another integer or if they enter a proper integer for it to print out the value at that index of the array – archer Mar 29 '16 at 03:27
  • There seems to be an issue with your second catch block. You should have another catch() statement before the second curly braces – Keno Mar 29 '16 at 03:27

1 Answers1

3

You were almost there. Try the following version.

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();
}
}
Sajeev
  • 818
  • 7
  • 23
  • It works if you enter a valid integer however it doesn't catch any exceptions it just crashes – archer Mar 29 '16 at 03:51
  • @archer I have updated the code to meet your requirements. – Sajeev Mar 29 '16 at 05:29
  • Your code still had some problems but I managed to do some more research and figure it out myself. Thanks for attempting to help – archer Mar 29 '16 at 08:09