0

I am trying to make a program that receives input from user, which is then used later on in the program. The problem I have is to make an exception. For example, if I only want the user to put in a number between 0 and whatever he previously put in. Here is an example:

    Scanner input = new Scanner(System.in); 
    System.out.println("What length do you want your array to be?");
    int length = input.nextInt(); 
    int[] arr = full(length);
    print(arr);
    System.out.println();
    System.out.println("Write a number between (0-"+length+"):");//Its here I want to add an exception.
    int number = input.nextInt();                       //if the user types in a greater value
    check(arr,number);                                  //of "length" or smaller value than 0
                                                        //I want the question to repeat itself
                                                        //until the criteria is met

You dont have to worry about the "check" or "print methods.

tomSurge
  • 246
  • 1
  • 3
  • 14
  • This could be useful: [`How to Create our own Exceptions in Java`](http://stackoverflow.com/a/1754473/3928341) as well as this [`Oracle Docs Article`](https://docs.oracle.com/javase/tutorial/essential/exceptions/) – nem035 Nov 21 '14 at 21:41
  • 2
    Do you have to build your own Exception class? Or can you simply throw a new Exception? – Drew Kennedy Nov 21 '14 at 21:41
  • I think there has been a missunderstanding, I meant a if/else or while statement which will not let the user type in values that does not meet the criteria. My english is not the best, so I apologize for that. – tomSurge Nov 21 '14 at 21:46
  • So you don't want to throw an exception, instead you just want to loop until the user inputs the right criteria? – Jonny Henly Nov 21 '14 at 21:47
  • An array really isn't necessary for this.. – Drew Kennedy Nov 21 '14 at 21:48
  • Yes, Johny. I have tried using if/else statements, but It wont work for me. – tomSurge Nov 21 '14 at 21:49
  • @DrewKennedy we don't know what he's doing with the array, since we don't have the code for the `full(int)` method. – Jonny Henly Nov 21 '14 at 21:49

2 Answers2

2

You do not need an exception, just add a do-while loop that checks whether it was a valid entry.

// ...

do
{
    System.out.println("Write a number between (0-"+length+"):");
    int number = input.nextInt();
} while( number < 0 || number >= length ); 

Note: I changed the condition to not include length since it appeared you were going to use that as an index to the array, and length is not a valid index in an array of length length.

clcto
  • 9,157
  • 17
  • 39
2

I don't believe you need an Exception, just a loop. Like a do-while loop such as

int number;
do {
    System.out.println("Write a number between (0-" + length + "):");
    number = input.nextInt();
    if (number < 0) {
        System.out.printf("%d is too low%n", number);
    } else if (number > length) {
        System.out.printf("%d is too high%n", number);
    }
} while (number < 0 || number > length);
check(arr, number);
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226