Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Search for number in array and return index

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Search for number in array and return index

    I have created an array with five elements and populated it. I am trying to find a specific number within the array and output the index. If the number is not found, the output should be "False".

    Sample:
    // Created array
    int[] iArray = {3,2,1,4,5};
     
    //I'm looking for 5 in the array
    int x1 = 5;
     
    //loop to find 5 in the array
    for (int i = 0; i < iArray.length; i++){
          if (iArray[i] == x1){
          int position = iArray[i];
          System.out.println("Index found: " + position);
          }
    }

    I know that the index is in fact 4, but when I run this code, it says 5. No matter what I change int x1 to, it will return the value of x1. What am I doing wrong?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Search for number in array and return index

    Duplicate post over at Java Forums. Already answered.

  3. The Following User Says Thank You to Junky For This Useful Post:

    Kevinius (March 24th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Search for number in array and return index

    I originally posted one this forum first. But yes, it was answered. I ended up tweaking it so that if there were multiple instances of that value, they would also be displayed. Thanks again.

Similar Threads

  1. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM
  2. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  3. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM
  4. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 05:04 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM