Results 1 to 7 of 7
  1. #1
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default Searching an array for a number and returning the index

    I've created an array (iArray) and initialized an int variable (x1) to 1. I'm trying to search the array for this number and return the index where it is found. The problem I'm having is that it returns 1...even though it's true index is 2. This is a sample of what I have:

    Java Code:
    int[] iArray = {3,2,1,4,5};
    
    int x1 = 1;
    
    for (int i = 0; i < iArray.length; i++){
           if (iArray[i] == x1){
           int position = iArray[i];
           System.out.println("Index found: " + position);
           }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What do you mean by return? Is this code inside a method? Is that method returning the correct value? Since you declared position variable inside the if statement you cannot access outside the if statement. In future post a SSCCE so people can see what you are really doing.

  3. #3
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    This is the a much larger part of the code:

    Java Code:
     public static void main(String[] args) {
    
            Scanner in = new Scanner(System.in);
            Random rNum = new Random();
    
            System.out.print("TYPES OF ARRAYS: "
                    + "\n 1. Integer"
                    + "\n 2. Float"
                    + "\n 3. Double"
                    + "\n 4. Long"
                    + "\n Enter a number for the correspsonding array type: ");
    
            /**
             * Checks user input for non-integer input.
             */
            while (!in.hasNextInt()) {
                System.out.println("'" + in.nextLine() + "'" + " was not a valid input! Try again: ");
            }
            int answer = in.nextInt();
    
            /**
             * Based on user input, there are four different arrays that can be
             * created.
             */
            switch (answer) {
                case 1:
                    /**
                     * New int[] iArray created and passed as an argument
                     * into new MyArray
                     */
                    int[] iArray = {3,2,1,4,5};
                    MyArray array = new MyArray(iArray);
                    /**
                     * iArray filled with random numbers
                     */
    
    //                for (int i = 0; i < n; i++) {
    //                    int x = rNum.nextInt(100);
    //                    iArray[i] = x;
    //                }
    
    
                    /**
                     * Displays the sum, average, highest value, and lowest
                     * value of instantiated intArray
                     */
                    System.out.println();
                    System.out.println("The sum is: " + array.getTotal(iArray));
                    System.out.println("The average is: " + array.getAverage(iArray));
                    System.out.println("The highest value is: " + array.getHighest(iArray));
                    System.out.println("The lowest value is: " + array.getLowest(iArray));
                    System.out.println("");
    
                    /**
                     * Prints instantiated intArray in ascending order
                     */
    //                System.out.println("Array in ascending order:");
    //                array.bubbleSortAscend(iArray);
    //                for (int i = 0; i < iArray.length; i++) {
    //                    System.out.println(i + "." + " " + iArray[i]);
    //                }
    //                System.out.println();
    
                    /**
                     * Prints instantiated intArray in descending order
                     */
    //                System.out.println("Array in descending order:");
    //                array.bubbleSortDescend(iArray);
    //                for (int i = 0; i < iArray.length; i++) {
    //                    System.out.println(i + "." + " " + iArray[i]);
    //                }
    
                    /**
                     * Generating a random number
                     */
                    int x1 = 5;
    //                System.out.println();
    //                int x1 = rNum.nextInt(100);
    //                System.out.println("Random number is: " + x1);
    
                    /**
                     * Search for random number
                     */
                     for (int i = 0; i < iArray.length; i++){
                         if (iArray[i] == x1){
                             int position = iArray[i];
                             System.out.println("Index found: " + position);
                         }
                     }
                     
                    
    
                    break;
                case 2:
                    /**
                     * New float[] fArray created and passed as an argument into
                     * new MyArray
                     */
                    float[] fArray = new float[n];
                    MyArray array1 = new MyArray(fArray);
                    /**
                     * fArray filled with random numbers
                     */
                    for (int i = 0; i < n; i++) {
                        float x = (rNum.nextInt(1000) + rNum.nextFloat());
                        fArray[i] = x;
                    }
                    /**
                     * Displays the sum, average, highest value, and lowest
                     * value of the instantiated floatArray
                     */
                    System.out.println();
                    System.out.println("The sum is: " + array1.getTotal(fArray));
                    System.out.println("The average is: " + array1.getAverage(fArray));
                    System.out.println("The highest value is: " + array1.getHighest(fArray));
                    System.out.println("The lowest value is: " + array1.getLowest(fArray));
                    System.out.println("");
    
                    /**
                     * Prints instantiated floatArray in ascending order
                     */
                    System.out.println("Array in Ascending order:");
                    array1.bubbleSortAscend(fArray);
                    for (int i = 0; i < fArray.length; i++) {
                        System.out.println((i + 1) + "." + fArray[i]);
                    }
                    System.out.println("");
    
                    /**
                     * Prints instantiated floatArray in descending order
                     */
                    System.out.println("Array in descending order:");
                    array1.bubbleSortDescend(fArray);
                    for (int i = 0; i < fArray.length; i++) {
                        System.out.println((i + 1) + "." + fArray[i]);
                    }
                    break;
                case 3:
                    /**
                     * New double[] dArray created and passed as an argument into new
                     * MyArray
                     */
                    double[] dArray = new double[n];
                    MyArray array2 = new MyArray(dArray);
    
                    /**
                     * dArray filled with random numbers
                     */
                    for (int i = 0; i < n; i++) {
                        double x = (rNum.nextInt(1000) + rNum.nextDouble());
                        dArray[i] = x;
                    }
    
                    /**
                     * Displays the sum, average, highest value, and lowest value of
                     * instantiated doubleArray
                     */
                    System.out.println();
                    System.out.println("The sum is: " + array2.getTotal(dArray));
                    System.out.println("The average is: " + array2.getAverage(dArray));
                    System.out.println("The highest value is: " + array2.getHighest(dArray));
                    System.out.println("The lowest value is: " + array2.getLowest(dArray));
                    System.out.println("");
    
                    /**
                     * Prints instantiated doubleArray in ascending order
                     */
                    System.out.println("Array in Ascending order:");
                    array2.bubbleSortAscend(dArray);
                    for (int i = 0; i < dArray.length; i++) {
                        System.out.println((i + 1) + "." + dArray[i]);
                    }
                    System.out.println("");
    
                    /**
                     * Prints instantiated doubleArray in descending order
                     */
                    System.out.println("Array in descending order:");
                    array2.bubbleSortDescend(dArray);
                    for (int i = 0; i < dArray.length; i++) {
                        System.out.println((i + 1) + "." + dArray[i]);
                    }
                    break;
                case 4:
                    /**
                     * New long[] lArray created and passed as an argument into 
                     * new MyArray
                     */
                    long[] lArray = new long[n];
                    MyArray array3 = new MyArray(lArray);
                    /**
                     * lArray filled with random numbers
                     */
                    for (int i = 0; i < n; i++) {
                        long x = rNum.nextLong();
                        lArray[i] = x;
                    }
    
                    /**
                     * Displays the sum, average, highest value, and lowest value of
                     * the instantiated longArray
                     */
                    System.out.println();
                    System.out.println("The sum is: " + array3.getTotal(lArray));
                    System.out.println("The average is: " + array3.getAverage(lArray));
                    System.out.println("The highest value is: " + array3.getHighest(lArray));
                    System.out.println("The lowest value is: " + array3.getLowest(lArray));
                    System.out.println("");
    
                    /**
                     * Prints instantiated longArray in ascending order
                     */
                    System.out.println("Array in Ascending order:");
                    array3.bubbleSortAscend(lArray);
                    for (int i = 0; i < lArray.length; i++) {
                        System.out.println((i + 1) + "." + lArray[i]);
                    }
                    System.out.println("");
    
                    /**
                     * Prints instantiated longArray in descending order
                     */
                    System.out.println("Array in descending order:");
                    array3.bubbleSortDescend(lArray);
                    for (int i = 0; i < lArray.length; i++) {
                        System.out.println((i + 1) + "." + lArray[i]);
                    }
                    break;
                default:
                    System.out.println("There are only four options. Run the program again and"
                            + "select an appropriate option.");
            }
        }
    }
    Pardon the parts that are commented out. The particular problem is in Case 1, and I will be duplicating the process in the other cases. I used the wrong choice of words when I said "return". I just want to print the index. The System.out.println is within the if statement.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You didn't bother to find out what SSCCE is did you.
    Java Code:
    int position = iArray[i];
    You set posistion to be the value in the array and not the index.

  5. #5
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Java Code:
    public class MyArrayDemo {
        public static void main(String[] args){
            int[] iArray = {3,2,1,4,5};
            int x1 = 1;
            for (int i = 0; i < iArray.length; i++){
                if (iArray[i] == x1){
                    int position = i;
                    System.out.println("Index found: " + position);
                    break;
                }
            }
        }
    }
    Is that what you were looking for? Understand that I'm not a pro like you...kindly explaining something would be nice. I was already frustrated with my current problem! I did make the change, and the correct index was shown. If I were to change x1 to a value not contained within the array, I know that a result will not be displayed (based on the code supplied). If I threw in an }else{ System.out.println("Not found."); I don't want to see that repeated the entire length of the array.
    Last edited by Kevinius; 03-24-2011 at 03:42 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Use a method and an if statement. Or a default value and an if statement after the loop.
    Java Code:
    int pos = -1;
    loop {
        code
    }
    if pos < 0 {
        print not found
    } else {
        print found at position
    }

  7. #7
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    This worked like a charm!

    Java Code:
    public class MyArrayDemo {
    
        public static void main(String[] args) {
            int[] iArray = {3, 2, 1, 4, 5};
            int x1 = 8;
    
            int position = -1;
            for (int i = 0; i < iArray.length; i++) {
                if (iArray[i] == x1) {
                    position = i;
                    break;
                }
            }
            if (position < 0) {
                System.out.println("False");
            } else {
                System.out.println("Found at index " + position);
            }
        }
    }
    I appreciate the help.

Similar Threads

  1. returning array
    By aizen92 in forum New To Java
    Replies: 4
    Last Post: 01-08-2011, 03:10 PM
  2. Finding maximum number at index position
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 10-27-2010, 08:14 PM
  3. Out of memory error while searching in index
    By priti_sh in forum Lucene
    Replies: 0
    Last Post: 05-05-2010, 11:16 AM
  4. Searching index with special chars
    By noorws in forum Lucene
    Replies: 1
    Last Post: 03-29-2010, 09:13 PM
  5. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •