+ 2
Can somebody please tell me where the mistake in this code is? the programm is telling me "missing return statement"
public static String mymethod (int a, int b, int c){ if (a > 5){ return "Typeone"; } else if (b < 5 && a <6 && a >3){ return "Typetwo"; } else if (c > 5 && a < 4){ return "Typethree"; } }
26 Antworten
+ 2
the method has String as a return value, so you must guarantee it returns a String
since you put every return value into a conditional, the compiler assumes that it may happen that none of the conditions is matched and, therefore, there will be no return value
+ 2
Got it, thanks!!
+ 2
you're welcome :)
+ 1
Just use ELSE at the end, without IF and return value for that situation
+ 1
Robby, same thing. if the compiler sees a conditional, he reads "under certain conditions, I might now have a return value", so he won't let you compile
+ 1
try with t.equals(s)
t==s won't to work with Reference types such as Strings.
I'd guess it tells you you lack return value because t==s won't produce true nor false with Strings
also, your code only checks the first String in the set before returning
+ 1
because String s == String t won't return true/false: the operator == doesn't work with strings 🎈
t.equals(s), on the contrary, will return true if the strings are equals
+ 1
post the code, we'll find out what's wrong :D
+ 1
if (t.equals(s)) {
if(ind == charSet.length) {
return false;
}
}
if t.equals(s) == true
but (ind !=CharSet.length), then the compiler is not sure what to return. You're basically saying "return false if this happens" and the compiler is asking "ok... but what do I do if that doesn't happen?"
if you want the function to return false only if BOTH those 2 conditions are matched, just go:
if(t.equals(s) && ind==charSet.length())
return false;
return true;
+ 1
I fear that only t.equals(s) being false would trigger that else :/
if you wanted that else to be triggered by ind==charSet.length, you should put it in the above standing curly brace (i. e. put line 13 and place it in line 16)
at that point, you'll be missing an "else" for the first "if", just put one and you're on 🎈
+ 1
Ok, let's just grab this from the bottom since I saw a couple things that confuse me
so, first of all, what is the code supposed to do?
+ 1
Oh, ok, in that case you can simply go:
public class Program {
static String[] charSet = {"abc","def","ghi"};
static boolean checkIfUsed(String s) {
if (Arrays.asList(charSet).contains(s))
return true;
return false;
}
}
this function returns true if charSet contains the string s.
otherwise it returns false
+ 1
Or, if you prefer a way to do it without "premade" functions, this also works:
public class Program {
static String[] charSet = {"abc","def","ghi"};
static boolean checkIfUsed(String s) {
for (int i = 0 ; i < charSet.length ; i++) {
if (s.equals(charSet[i]))
return true;
}
return false;
}
public static void main(String args[]) {
System.out.println("Does charSet contain abc? " + checkIfUsed("abc")); //outputs true
System.out.println("Does charSet contain ple? " + checkIfUsed("ple")); //outputs false
}
}
+ 1
you're welcome, always happy to help 🎈
don't worry, in the beginning one may tend to overthink but often the solution is simpler than it seems :) it only takes a bit of practice
+ 1
utnal si toso😎
0
Hi, I have pretty much the same issue here, yet in my case I have a conditional inside an enhanced for loop, and I'm returning a boolean. It keeps telling me that the method needs to return a boolean.
0
So where do I put the return block?
my method goes smth like this:
static boolean myMethod(String s) {
for (.......) {
if (......) {
return false;
}
else {
return true;
}
}
}
0
can you be a bit more detailed about your code?
0
Sorry my laptop was not on, here it goes:
static boolean checkIfUsed(String s) {
for (String t:charSet) {
if (t == s) {
return false;
}
else {
return true;
}
}
}