1

I need to send an integer array and a string array to this generic method and find out if a certain number or string is present there or not.I wrote this code but it is giving an error on the line if(e==30) saying that "Incompatible operand types E and int". Please help.

public class Ch2Lu3Ex2 { public static <E> void searchArray(E[] inputArray) { for(E e : inputArray) { if(e==30) { System.out.println("Element found in integer array"); } else if(e=="raj") { System.out.println("Element found in string array"); } } } public static void main(String[] args) { Integer[] integerArray = {10,20,30}; String[] stringArray = {"robin","raj","ravi"}; searchArray(integerArray); searchArray(stringArray); } } 
1
  • There are many things wrong here, fist of them being that you should (almost) never compare objects using == but with equals(), and secondly you don't need generics at all for this. Commented Jan 10, 2013 at 11:49

3 Answers 3

3

The problem is that you don't know if e is an Integer or a String, and you can't compare a String with an Integer and vice versa.

One solution would be to pass the sought item to your method too - it could look like this:

public static <E> void searchArray(E[] inputArray, E soughtItem) { for (E e : inputArray) { if (e.equals(soughtItem)) { System.out.println("Element found in integer array"); } } } 

And in your main code:

searchArray(integerArray, 30); searchArray(stringArray, "raj"); 

Also note that you should use equals instead of == for equality tests.

Finally, all this has already been written by others:

Set<String> set = new HashSet<String> (stringArray); if (set.contains("raj")) System.out.println("Found raj"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Or simply: public static void searchArray(Object[] inputArray, Object soughtItem)
@biziclop your method can then be called: searchArray(someIntegerArray, "ABC"); which is probably something you want to prevent at compile time.
1

There are two mistakes, both of them must be fixed:

1) fix it like: if(e instanceof Integer && (Integer)e==30) - you have to check, that e is an instance of Integer

2) Strings must be compared using equals method:

else if(e.equals("raj"))

3 Comments

Integer should be compared with equals too.
@assylias, I don't agree. In this case comparing with (int) constant, so it is ok.
Good point, the Integer will get unboxed and == will compare ints.
1

Don't use "==" when you compare objects! Change to "equals()" method and should work!

public class Ch2Lu3Ex2 { public static <E> void searchArray(E[] inputArray) { for(E e : inputArray) { if(e.equals(30)) { System.out.println("Element found in integer array"); } else if("raj".equals(e)) //This way no null pointer will occure { System.out.println("Element found in string array"); } } } public static void main(String[] args) { Integer[] integerArray = {10,20,30}; String[] stringArray = {"robin","raj","ravi"}; searchArray(integerArray); searchArray(stringArray); } } 

1 Comment

+1 for the null-pointer observation. Why not apply the same logic to the e.equals(30)? You will get a null-pointer there too, and also before it hits the second comparison :-P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.