Java - An array of int that will return Boolean and the index -


Is Java a way to see an array of integers for a specific user input and returns the boolean value And the index is also this? My method but I also wanted to know which index got this exact integer

code snippet:

  int numUser = Integer.parseInt (inputUser); Boolean IPhone = Incorrect; {If (sum [x] == numUser} for {int x = 0; x & lt; = 4; x ++} {ispresent = true; } Else {ispresent = false; }} If (ispress == true) {System.out.println ("number is in the array"); } Else {System.out.println ("number is not in array"); }   

Your problem is very specific index () This is usually a conference to return -1 when the element is not found, and the index (which is greater than or equal to 0) otherwise.

You have different ways to get that result.

Convert your list to a list
  int numUser = Integer.parseInt (inputUser); Int Index = Eraseus List (Yoga). IndexUser; If (index & lt; 0) {System.out.println ("number is not in array"); } Else {System.out.println ("number is in the array:" + index); }  

Use Apache ArrayUtils

  int numUser = Integer.parseInt (inputUser); Int index = ArrayUtils.indexOf (zodiac, numUser); If (index & lt; 0) {System.out.println ("number is not in array"); } Else {System.out.println ("number is in the array:" + index); }  

Type it yourself

  int numUser = integer. Excerpt (InputUser); Int index = -1; For (int i = 0; i  

If you do not talk about converting to list , then I use the first method (clear code with least dependencies) Will do If you pay attention and are already using Apache Utilities in your project, then the other way is fine, and avoid the conversion. If you want to have little dependence and are fine with more complex source code, then the third way can be what you are doing!


Comments