Results 1 to 14 of 14
  1. #1
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default Simple array question..

    So I'm trying to create a program that will go through a one dimensional array of numbers (10,9,8,7,6,5,4,3,2,1,0). I need the program to print the index of which an inputted number is at - e.g If the user inputs 9, it will print 1 (because 9 is at the index 1.) The problem I am having is that I need it to print -1 if a number entered is NOT in the array.. I am kind of confused as to how I can do this, any tips are appreciated.. thanks

    Here is the code I have so far.. (unfinished)

    Java Code:
        static int findIndexOf (int[] numbers, int target)
        {
            int index = 0;
    
            for (index = 0 ; index < numbers.length ; index++)
            {
                if (numbers [target] != index)
                {
                    c.println (index);
    
                }
                else
                {
                    c.println ("The number is at position: " + numbers [target]);
                }
           }
            return index;
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    if (numbers [target] != index)

    Shouldn't that be

    Java Code:
    if (numbers[index] != target)

  3. #3
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Shouldn't that be

    Java Code:
    if (numbers[index] != target)
    When I run it like that, I get the output

    Java Code:
    0
    The number is at position: 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    The number is at position: 1
    If I enter a number say 9999, I will get an ArrayIndexOutOfBoundsException..

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Post a short runnable example (SSCCE) which results in the exception.

  5. #5
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Post a short runnable example (SSCCE) which results in the exception.
    I don't really understand what you mean by this, are you asking me to post a short amount of code that results in the error? Can't you get that from my first post?

  6. #6
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    i believe you changed the above but probably didn't fix the print statement:

    Java Code:
    System.out.println ("The number is at position: " + numbers [target]);
    over here target probably needs to be changed to index.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The code you posted is not runnable.

    Yes, I am puzzled by the claim that using the condition 'if(numbers[index] != target)' rather than the line you had will result in an AIOOBE. So it would be helpful to see code illustrating that.

    [Edit] Ah! f1gh is probably right!

  8. #8
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    The code you posted is not runnable.

    Yes, I am puzzled by the claim that using the condition 'if(numbers[index] != target)' rather than the line you had will result in an AIOOBE. So it would be helpful to see code illustrating that.
    Here is everything I have, its not long just a simple program

    Java Code:
    import java.awt.*;
    import hsa.Console;
    
    public class Array_Assignment_1
    {
        static Console c;           // The output console
    
        public static void main (String[] args)
        {
            c = new Console ();
    
            int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
            int target = 0;
            int findIndex = 0;
    
            c.println ("Please enter the number you wish to find the index of. ");
            target = c.readInt ();
    
            findIndex = findIndexOf (numbers, target);
            //c.println ("The number is at position: " + numbers [target]);
        }
    
    
        static int findIndexOf (int[] numbers, int target)
        {
            int index = 0;
    
            for (index = 0 ; index < numbers.length ; index++)
            {
                if (numbers [index] != target)
                {
                    c.println (index);
                }
                else
                {
                    c.println ("The number is at position: " + numbers [index]);
                }
            }
            return index;
        }
    }

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    findIndexOf() is not finding the index of the target. It is always returning numbers.length because that is what finishes the for loop.

    It would be better to returning index as soon as numbers[index] equals target. If you get all the way through the for loop without finding a match, that is the time to return -1.

    (I'm guessing you get the AIOOBE at the line you commented out. That makes sense because findIndexOf() returns numbers.length and when you try and use that as an index you will get an exception as it's too big.)
    Last edited by pbrockway2; 04-22-2011 at 03:21 AM.

  10. #10
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    findIndexOf() is not finding the index of the target. It is always returning numbers.length because that is what finishes the for loop.

    It would be better to returning index as soon as numbers[index] equals target. If you get all the way through the for loop without finding a match, that is the time to return -1.

    (I'm guessing you get the AIOOBE at the line you commented out. That makes sense because findIndexOf() returns numbers.length and when you try and use that as an index you will get an exception as it's too big.)
    I can't really seem to figure this out.... are you saying make the for loop:

    Java Code:
    for (index = 0 ; numbers[index] == target ; index++)
    Or something similar, I don't really understand this.. That loop I just printed would start at the index of 0, and continue until the value of index in the numbers array is equal to target, and if not it will add one to index, and continue until it does equal target?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    No, the for loop initialisation etc is correct. There is no reason to change it.

    The thing is in the body of the for loop. You are trying to return the index vcalue that matches target right? So do that: remove all the println() stuff and return the value.

    Java Code:
    for (index = 0 ; index < numbers.length ; index++)
    {
        if (numbers [index] == target)
        {
            return index;
        }
    }

  12. #12
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    No, the for loop initialisation etc is correct. There is no reason to change it.

    The thing is in the body of the for loop. You are trying to return the index vcalue that matches target right? So do that: remove all the println() stuff and return the value.

    Java Code:
    for (index = 0 ; index < numbers.length ; index++)
    {
        if (numbers [index] == target)
        {
            return index;
        }
    }
    This still dosent seem to be working for me, I replaced my for loop, with that and still get an AIOOBE error, and for some reason it dosent seem to be letting me have the return, inside the if statement?

  13. #13
    Arigo is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    When trying to run this

    Java Code:
        static int findIndexOf (int[] numbers, int target)
        {
            int index = 0;
    
            for (index = 0 ; index < numbers.length ; index++)
            {
                if (numbers [index] == target)
                {
                    return index;
                }
            }
        }
    }
    I get a compile error saying Method findIndexOf must contain a return statement...

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Every possible "path of execution" must return a value. At the moment the method returns a value (index) when a match is found between numbers[index] and target but the method must also return something if no match is found: ie if the for loop gets all the way through because target is not present in the array.

    Java Code:
    static int findIndexOf (int[] numbers, int target)
    {
        int index = 0;
    
        for (index = 0 ; index < numbers.length ; index++)
        {
            if (numbers [index] == target)
            {
                return index;
            }
        }
        [color=green]// you must return some value here (indicating that target was not in the array)[/color]
    }

Similar Threads

  1. Replies: 6
    Last Post: 02-17-2011, 02:22 PM
  2. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 06:35 PM
  3. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 06:32 PM
  4. pretty simple array question
    By 711groove in forum New To Java
    Replies: 8
    Last Post: 12-06-2009, 11:14 AM
  5. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM

Posting Permissions

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