i have a task to complete. I have to create a return type method takes String array as an argument and returns to int. So my method works completely fine i will use it to count multiple elements of an element in an array but on method name it says “Return value of the method is never used”. I just didn’t get what am i missing on method. This is my method:
public static int countMultiple(String[] s){ int counter = 0; for (int i = 0; i < s.length; i++) { s[i] = s[i].trim(); if (s[i].contains(" ")) { counter++; } } System.out.println(counter); return counter; }
Advertisement
Answer
I assume what you mean is not really an error, but rather a warning from your IDE. Your IDE basically wants to tell you, that you return a value with your method (in your case an int value), but never use this value to work with it.
Therefore, the IDE suggests you improve your code, as if you never need the value you could also just return a void value. Therefore you have two ways to solve this issue:
a) In the method from where you call countMultiple(String[] s)
you can work with that value, e.g. by simply logging it.
b) If you don’t need the value in the caller method, you can return void: public static void countMultiple(String[] s)
For case b
, think of removing return counter;
from the method as well.