java stuck 8-)

Discussion in 'OT Technology' started by sunnyside, Aug 21, 2005.

  1. sunnyside

    sunnyside Guest

    so here's the code...what im trying to do is this...if the input is out of the array index (0,1,2) i want to throw an exception at the user BUT give the user another chance to enter another input. as of right now, it just throws an exception and tells the user that the answer isnt good. I dont know where to put the while loop...or if i even use a while loop...any help is appreciated

    public class TryException {
    public static void main(String [] args){
    int value;
    String input = JOptionPane.showInputDialog
    ("Enter 0, 1, 2 or -1 to quit");

    int index = Integer.parseInt(input);

    int [ ] anArray = {5,6,7};
    value = anArray[index];
    System.out.println("Execution does not get here if index is bad");
    catch(ArrayIndexOutOfBoundsException e)
    System.out.println("Stick with 0,1,2");
    System.out.println("This is the end of the program");
  2. sam758

    sam758 OT Supporter

    Aug 26, 2003
    Likes Received:
    while loop around the try and catch blocks

Share This Page