Results 1 to 14 of 14
Thread: Simple array question..
- 04-22-2011, 01:40 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
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; } }
- 04-22-2011, 01:43 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
if (numbers [target] != index)
Shouldn't that be
Java Code:if (numbers[index] != target)
- 04-22-2011, 01:49 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
- 04-22-2011, 01:56 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Post a short runnable example (SSCCE) which results in the exception.
- 04-22-2011, 02:02 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
- 04-22-2011, 02:07 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
i believe you changed the above but probably didn't fix the print statement:
over here target probably needs to be changed to index.Java Code:System.out.println ("The number is at position: " + numbers [target]);
- 04-22-2011, 02:11 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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!
- 04-22-2011, 02:12 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
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; } }
- 04-22-2011, 02:18 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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 02:21 AM.
- 04-22-2011, 02:35 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
I can't really seem to figure this out.... are you saying make the for loop:
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?Java Code:for (index = 0 ; numbers[index] == target ; index++)
- 04-22-2011, 02:48 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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; } }
- 04-22-2011, 02:58 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
- 04-22-2011, 03:13 AM #13
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
When trying to run this
I get a compile error saying Method findIndexOf must contain a return statement...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; } } } }
- 04-22-2011, 03:18 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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
-
A simple question about converting byte array to unicode string
By Genom in forum New To JavaReplies: 6Last Post: 02-17-2011, 01:22 PM -
Simple Question
By stackptr89 in forum New To JavaReplies: 13Last Post: 01-29-2011, 05:35 PM -
some simple question?
By jperson in forum New To JavaReplies: 4Last Post: 05-03-2010, 05:32 PM -
pretty simple array question
By 711groove in forum New To JavaReplies: 8Last Post: 12-06-2009, 10:14 AM -
Simple Question
By barusk in forum NetworkingReplies: 13Last Post: 03-04-2009, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks