Reputation: 9
public static double Num (JTextField field){
String s1 = field.getText();
String n = new String ("N");
String e = new String ("E");
String w = new String ("W");
String s = new String ("S");
if (s1.equalsIgnoreCase(n)){
return 1;}
else if (s1.equalsIgnoreCase(e)){
return 1;}
else if (s.equalsIgnoreCase(s)){
return -1;}
if (s1.equalsIgnoreCase(w)){
return -1;}
}
Hi I'm a newbie in Java programming. Well i justed wanted to return specific values from a double. I'm attaching the part of the code.I 've selected double because I need to use its value in later sections of the main class code.
After this I'm getting an error saying
This method must return a result of type double.
And the program is suggesting to add (yet another) return in the end.
Upvotes: 0
Views: 3993
Reputation: 163
public static double Num (JTextField field){
String s1 = field.getText();
String n = new String ("N");
String e = new String ("E");
String w = new String ("W");
String s = new String ("S");
if (s1.equalsIgnoreCase(n)){
return 1;}
else if (s1.equalsIgnoreCase(e)){
return 1;}
else if (s.equalsIgnoreCase(s)){
return -1;}
if (s1.equalsIgnoreCase(w)){
return -1;}
return 0;
}
You Will Need to return some value. As provided in the example above you will need to return at least some value. Otherwise you can use void instead of double.
Upvotes: 0
Reputation: 2039
If you declare a return type in the methods signature, you always need to return something of that type.
In your case, if the text is neither N, E, W, or S then nothing would be returned. Java does not allow this.
If the text should always be one of those values throw an exception and handle it in the calling code. otherwise return a default value.
You do not need to declare the return type as double because you need double values later on. Just return an int, it will be implicitly cast to a double anyways.
Also, you do not need to create a String object for everything, this just blows up your code unneccessay. Just write: if (s1.equalsIgnoreCase("N")
Upvotes: 0
Reputation: 2335
You must add a return statement outside all if/else if statements. The compiler gives the error because while compilation it finds that it is possible for this program not to return any value in case none of the if statements get satisfied.
According to your logic, you might assume that one of the if statements will be definitely satisfied but compiler does not know that. So you should add a fake return value, (like return 0) so that error is not given by compiler.
Upvotes: 2
Reputation: 9445
You need to handle the case that neither of the letters you specified equals the field's text. In this case, none of your four return
statement is called, and Java doesn't know what value to return. The solution is - as the compiler already suggested - to add a return
statement to the end of the method body, which returns the default value.
Upvotes: 1
Reputation: 2381
1 and -1 represent Integers. It expects to return a float, not an int. If you wish to return an int instead, change the "double" to "int":
public static int Num (JTextField field){
Alternatively, if you want to return a float, add ".0" to the return values;
return 1.0;
return -1.0;
The reason it tells you it needs another return statement at the end of the method is because it must return something, and there's no way to be sure that your method returns something. If you know one of the if's will always be true, you can change the last if to an else:
if (s1.equalsIgnoreCase(n))
return 1.0;
else if (s1.equalsIgnoreCase(e))
return 1.0
else if (s.equalsIgnoreCase(s))
return -1.0;
else
return -1.0;
If they could all be false at the same time, you must add another return at the end to make sure to return a value;
if (s1.equalsIgnoreCase(n))
return 1.0;
else if (s1.equalsIgnoreCase(e))
return 1.0
else if (s.equalsIgnoreCase(s))
return -1.0;
else if (s.equalsIgnoreCase(w))
return -1.0;
return 0.0;
Upvotes: 1
Reputation: 72755
I don't know Java but these look like the problems.
You're returning an integer in many places. I think it should be a 1.0
or something similar for Java to let you return it. Also, you don't have an return in case non of the if/else conditions match. This means that it's possible for your function to not return a double which is also a potential error. That's the suggestion.
Upvotes: 0