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");
}
6
  • Create a new instance of class with int index and boolean flag as instance variables and return this object. Commented Feb 18, 2014 at 13:23
  • 4
    Please don't name your variable Boolean. This is very very bad style. Commented Feb 18, 2014 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} ` Commented Feb 18, 2014 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. Commented Feb 18, 2014 at 13:34
  • possible duplicate of Where is Java's Array indexOf? Commented Feb 18, 2014 at 13:38

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

2 Comments

thanks! I used the third one and it worked perfectly. I really need the index to manipulate another array :))
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! ;)
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.

Comments

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.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.