10 Replies - 1041 Views - Last Post: 09 October 2012 - 11:27 AM Rate Topic: -----

#1 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

MISSING RETURN STATEMENT

Posted 09 October 2012 - 10:53 AM

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";

Is This A Good Question/Topic? 0
  • +

Replies To: MISSING RETURN STATEMENT

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: MISSING RETURN STATEMENT

Posted 09 October 2012 - 10:58 AM

Can you please post the exact errors? Helps a lot.
Was This Post Helpful? 0
  • +
  • -

#3 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: MISSING RETURN STATEMENT

Posted 09 October 2012 - 11:01 AM

View Postjdavi134, 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
Was This Post Helpful? 0
  • +
  • -

#4 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

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.
Was This Post Helpful? 0
  • +
  • -

#5 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

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.

This post has been edited by Zoquo: 09 October 2012 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: MISSING RETURN STATEMENT

Posted 09 October 2012 - 11:09 AM

View PostZoquo, 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 ...
Was This Post Helpful? 0
  • +
  • -

#7 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

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.
Was This Post Helpful? 0
  • +
  • -

#8 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

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 ;(
Was This Post Helpful? 0
  • +
  • -

#9 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

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.
Was This Post Helpful? 0
  • +
  • -

#10 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

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";
}

This post has been edited by grimpirate: 09 October 2012 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#11 javalotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: MISSING RETURN STATEMENT

Posted 09 October 2012 - 11:27 AM

View Postgrimpirate, on 09 October 2012 - 11:24 AM, said:

At the end of your method append the following:
} else {
return inputMessage+"\n"+"Unknown command";
}


thank you so much :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1