0

Is there a way for java to check an array of integers for a specified user input and return boolean value and also the index where it resides? I have my way but I also wanted to know which index did it found the exact integer

Snippet of the code:

int numUser = Integer.parseInt(inputUser);
boolean ispresent = false;

for(int x = 0; x<=4; x++){
    if(sum[x] == numUser){
        ispresent = true;
    }else{
        ispresent = false;
    }
}

if(ispresent== true){
    System.out.println("The number is in the array");
}else{
    System.out.println("The number is not in the array");
}
Sean Mira
  • 5
  • 3
  • Create a new instance of `class` with `int index` and `boolean flag` as instance variables and return this object. – Aniket Thakur Feb 18 '14 at 13:23
  • 4
    Please don't name your variable `Boolean`. This is very very bad style. – Maroun Feb 18 '14 at 13:24
  • The expression `sum[x] == numUser` is already of type boolean. You don't need to assign it to a a variable. You can simply do: `if(sum[x] == numUser)` { //do what you do if true} ` – Edwin Dalorzo Feb 18 '14 at 13:28
  • This code is actually wrong. The ``else { ispresent = false; }`` should be deleted, as it may overwrite the ``ispresent`` value when you find ``numUser``! Currently, your code tests if ``numUser`` is present at the last position of the array. – gdiazc Feb 18 '14 at 13:34
  • possible duplicate of [Where is Java's Array indexOf?](http://stackoverflow.com/questions/4962361/where-is-javas-array-indexof) – snooze92 Feb 18 '14 at 13:38
  • Thank you for the answer guys, and @snooze92 , nope. its not. :)) I'm new to this site, also new to java – Sean Mira Feb 18 '14 at 13:42

6 Answers6

3

You could maybe return an -1 for when it doesn't find it in the array. Since there is no -1 index in an array, you can tell that it does not appear in the array.

If the value does appear in the array, you can return that index. Since you are returning an int always.

Student
  • 195
  • 1
  • 12
1

Your problem is the very typical indexOf(). It is usually a convention to return -1 when the element was not found, and the index (which is greater or equal to 0) otherwise.

You have different ways to get to that result.

Convert your array to a List

int numUser = Integer.parseInt(inputUser);
int index = Arrays.asList(sum).indexOf(numUser);
if (index < 0) {
    System.out.println("The number is not in the array");
} else {
    System.out.println("The number is in the array: " + index);
}

Use Apache ArrayUtils

int numUser = Integer.parseInt(inputUser);
int index = ArrayUtils.indexOf(sum, numUser);
if (index < 0) {
    System.out.println("The number is not in the array");
} else {
    System.out.println("The number is in the array: " + index);
}

Write it yourself

int numUser = Integer.parseInt(inputUser);
int index = -1;
for (int i = 0; i < sum.length; ++i) {
    if (sum[i] == numUser) {
        index = i;
        break;
    }
}
if (index < 0) {
    System.out.println("The number is not in the array");
} else {
    System.out.println("The number is in the array: " + index);
}

If you don't mind converting to a List, I would use the first method (clearer code with least dependencies). If you do mind and are already using Apache Utils in your project, the second method is fine, and avoid the conversion. If you want to keep as little dependencies and are fine with more complex source code, the third method might be what you are after!

snooze92
  • 4,178
  • 2
  • 29
  • 38
  • thanks! I used the third one and it worked perfectly. I really need the index to manipulate another array :)) – Sean Mira Feb 18 '14 at 13:44
  • You're welcome. Let me know if you want me to explain the code further. If you are a Java beginner, perhaps everything is not as obvious as I think it is? Note for instance that all three methods do give you the index, and that if you have no good reason to avoid converting your Array to a List, the first solution gives you a code easier to understand! ;) – snooze92 Feb 18 '14 at 13:56
0

You could use:

int result = Arrays.asList(array).indexOf(value);
boolean contained = result != -1;

A result of -1 means, that the value is not in the array; in case result >= 0, it is the actual index.

qqilihq
  • 10,794
  • 7
  • 48
  • 89
0

Use

int numUser = Integer.parseInt(inputUser);
int indexOfNumUser = Arrays.asList(sum).indexOf(numUser);

if the numUser exists then indexOfNumUser contains the index of the num in the array. If not it contains -1.

René Link
  • 48,224
  • 13
  • 108
  • 140
0

You also could try to replace array of Integers with ArrayList, if you do not have use it. This will make your task much easier to solve.

baga
  • 70
  • 1
  • 5
0

Strictly in "standard" Java, you should use the answers posted before (convert to List and use indexOf). But if you want to stick with the Array, use from ApacheCommons ArrayUtils.indexOf(int[], int).

From the Documentation:

Returns:
the index of the value within the array, INDEX_NOT_FOUND (-1) if not found or null array input
yamilmedina
  • 3,315
  • 2
  • 20
  • 28