3 Replies Latest reply: Feb 23, 2009 1:17 PM by 843785 RSS

    How to test if input is in array + number?

    843785
      Hi, I have an array, [0, 1, 2], and also a stored number: 5

      If I ask the user to input a number, how do I make the user continue input until the number is either one of the numbers in the array or the stored number?

      I can do this with a while loop, a for loop, and an if, else loop, but it's too complicated:
                     boolean match = false;
                
                     DONE: while (match != true) {
                          for (int i =0; i < number; i++) {
                               if (userInput == array[i] || userInput == -1) {
                                    match = true;
                                    break DONE;
                               } else {
                                    match = false;
                               }
                          }
      
                          System.err.printf("\nERROR: Input not in array or stored number.\n\n");
                          System.out.printf("Input again:\n    ");
                          userInput = input.nextInt();
                     }
      Is there an easier method? Could someone point me in the right direction? I'm wondering if it might be faster if I create a new array that includes the original array + the stored number, then test to see if the input is simply in this new array.

      Thanks for any help.

      Edited by: fishy on Feb 23, 2009 11:16 AM
        • 1. Re: How to test if input is in array + number?
          843785
          Hello,

          To check the number against the array, you would have to loop through the entire array checking each element for the input value. A for loop should do the trick:
          private static int checkArray(int [] a, int inputNumber)
          {
          for (i = 0; i < a.length; i++) {  
           if (a[i] == inputNumber)
                return 1;
          }
           return 0;
          }
          Then you can use a do while for the input:
          do {
             System.out.println("Enter a number: ");
             userInput = sc.nextInt; 
          } while ( (checkArray(userInput) == 0) & (number != userInput) ); 
          This should ask the user for input as long as the number they enter is not in the array and it's not the stored number.

          Hope this helps,

          ~Vin
          • 2. Re: How to test if input is in array + number?
            843785
            It's always good to break algorithms down into steps. For example, searching the array:
            public boolean contains(int[] array, int searchValue) {
                for(int value : array) {
                    if (value == searchValue) {
                        return true;
                    }
                }
                return false;
            }
            If you are unfamiliar with that loop, it's an enhanced for loop: [http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html]

            I wouldn't work about "faster" unless your array contains millions of elements, and then you would be using a hash table or sorting and using binary search.

            By advice is to write the cleanest code you can.
            • 3. Re: How to test if input is in array + number?
              843785
              Thank you for the replies!