Results 1 to 7 of 7
- 03-24-2011, 02:42 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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); } }
- 03-24-2011, 02:47 AM #2
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.
- 03-24-2011, 03:01 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
This is the a much larger part of the code:
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.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."); } } }
- 03-24-2011, 03:08 AM #4
You didn't bother to find out what SSCCE is did you.
You set posistion to be the value in the array and not the index.Java Code:int position = iArray[i];
- 03-24-2011, 03:40 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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.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; } } } }Last edited by Kevinius; 03-24-2011 at 03:42 AM.
- 03-24-2011, 03:49 AM #6
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 }
- 03-24-2011, 04:01 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
This worked like a charm!
I appreciate the help.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); } } }
Similar Threads
-
returning array
By aizen92 in forum New To JavaReplies: 4Last Post: 01-08-2011, 03:10 PM -
Finding maximum number at index position
By Shyamz1 in forum New To JavaReplies: 9Last Post: 10-27-2010, 08:14 PM -
Out of memory error while searching in index
By priti_sh in forum LuceneReplies: 0Last Post: 05-05-2010, 11:16 AM -
Searching index with special chars
By noorws in forum LuceneReplies: 1Last Post: 03-29-2010, 09:13 PM -
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks