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: Searching an array for a certain value reporting back index position

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Searching an array for a certain value reporting back index position

    I have not experimented with binary searches and things like that so my code is using a simpler way. The value I'm searching for is 9 which is found at index position 4 but my program is reporting that the value is found at index position 9. There is something I'm doing wrong here:

    public class SearchArrayForValue
    {
    	public static void main(String args[])
    	{
    		int numbers[] = {2, 5, 7, 8, 9, 11};
    		int value = 9;
    		int i;
    		int j;
     
    		System.out.print("The values in the array are: ");
     
    		for(i = 0; i < 6; i++)
    			System.out.print(numbers[i] + " ");
     
    		for(j = 0; j < 6; j++)
     
    			if(numbers[j] == value)
    				break;
     
    		System.out.println("\nValue of " + value + " found at index position " + numbers[j]);	
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Searching an array for a certain value reporting back index position

    numbers[j] is the value in the array at index j


    Note: The code should use {}s to enclose the statements inside of loops and if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Searching an array for a certain value reporting back index position

    Quote Originally Posted by Norm View Post
    numbers[j] is the value in the array at index j


    Note: The code should use {}s to enclose the statements inside of loops and if statements.
    I experimented and discovered I only need to use the loop control variable in this case j to output the index position.

Similar Threads

  1. Searching a 2d Array for lines of 3
    By Lurie1 in forum Loops & Control Statements
    Replies: 4
    Last Post: January 20th, 2014, 02:00 PM
  2. How to find the array index of the lowest value in the array?
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2013, 06:57 AM
  3. [SOLVED] Help how do I get the position out of an array
    By codyjava in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 2nd, 2012, 04:35 PM
  4. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM