I cannot understand where is my mistake......help me please ;(
package scvprocessor.another.version;
import java.util.Scanner;
/**
*
* @author c2rsaldi
*/
public class SCVProcessorAnotherVersion {
static Scanner message = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Type a command");
String inputMessage = message.nextLine();
inputText (inputMessage) ;
System.out.println(inputText (inputMessage));
// TODO code application logic here
}
public static String inputText (String inputMessage) {
// here is a mistake (missing return statement)
if (inputMessage.contains("print")) {
return inputMessage +"\n"+"Print Command";
} else if (inputMessage.contains("save")) {
return inputMessage + "\n"+"Save Command";
}
else if (inputMessage.contains("=")) {
return inputMessage+"\n"+"Assignment command";
} else if (inputMessage.contains("exit")) {
return inputMessage+"\n"+"Exit command";
MISSING RETURN STATEMENT
Page 1 of 110 Replies - 338 Views - Last Post: 09 October 2012 - 11:27 AM
Replies To: MISSING RETURN STATEMENT
#2
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 10:58 AM
Can you please post the exact errors? Helps a lot.
#3
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:01 AM
jdavi134, on 09 October 2012 - 10:58 AM, said:
Can you please post the exact errors? Helps a lot.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at scvprocessor.another.version.SCVProcessorAnotherVersion.inputText(SCVProcessorAnotherVersion.java:24)
at scvprocessor.another.version.SCVProcessorAnotherVersion.main(SCVProcessorAnotherVersion.java:20)
Java Result: 1
#4
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:04 AM
Is that all of your code? And also, repost your code in code brackets. I'm having trouble reading it with my terrible eyes.
#5
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:04 AM
The problem is that there is a chance that your method will not meet the conditions to return a string. You must have at least one return statement that will always return something.
If I pass a string that contains none of those strings, what happens? That's what you need to answer.
If I pass a string that contains none of those strings, what happens? That's what you need to answer.
This post has been edited by Zoquo: 09 October 2012 - 11:09 AM
#6
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:09 AM
Zoquo, on 09 October 2012 - 11:04 AM, said:
The problem is that there is a chance that your method will not meet the conditions to return a string. You must have at least one return statement that will always return something.
package scvprocessor.another.version;
import java.util.Scanner;
public class SCVProcessorAnotherVersion {
static Scanner message = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Type a command");
String inputMessage = message.nextLine();
inputText (inputMessage) ;
System.out.println(inputText (inputMessage));
}
public static String inputText (String inputMessage) {
if (inputMessage.contains("print")) {
return inputMessage +"\n"+"Print Command";
} else
if (inputMessage.contains("save")) {
return inputMessage + "\n"+"Save Command";
}
else
if (inputMessage.contains("=")) {
return inputMessage+"\n"+"Assignment command";
} else
if (inputMessage.contains("exit")) {
return inputMessage+"\n"+"Exit command";
}
}
}
sorry, I am new in Java ;( I have 4 RETURNS in method inputText. I thought that is enough ...
#7
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:13 AM
The problem isn't that you don't have a return statement. The problem is that there is a chance that the method is passed a string that meets none of those conditions. In that case, none of the return statements would execute.
#8
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:16 AM
so how its better to fix it? i spent 3 hours..sorry,i really dont know ;(
#9
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:21 AM
There are numerous ways to fix the problem. One is to have a return statement that has no condition. Since I don't know the overall purpose of your program, I cannot really fix your code.
#10
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:24 AM
At the end of your method append the following:
} else {
return inputMessage+"\n"+"Unknown command";
}
} else {
return inputMessage+"\n"+"Unknown command";
}
This post has been edited by grimpirate: 09 October 2012 - 11:24 AM
#11
Re: MISSING RETURN STATEMENT
Posted 09 October 2012 - 11:27 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|