wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Whats wrong with this code? public int method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whats wrong with this code? public int method" Watch "Whats wrong with this code? public int method" New topic
Author

Whats wrong with this code? public int method

Sara Lyons
Greenhorn

Joined: Dec 05, 2012
Posts: 14
Whats wrong with this code? I haven't a clue about public int method
Don't change anything inside the method.
When I tried to compile it it said :

Study.java:17: error: missing return statement
}
^
1 error

class Study
{
private int strange;
public int strange(String sentence)
{
boolean isSpace = false;
int count = 0;
for (int i = 1; i < sentence. length ( ) ; i++)
{
isSpace = (sentence.charAt(i) == ' ');
if(isSpace&&sentence.charAt(i-1) == 'e')
{
count = count + 1;
return count;
}
}
}//this is line 17
}
Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
The method is declared to return an "int" - so it must return an int under all circumstances. As it is, it may reach the end of the for loop - where there is no return statement, so the JVM doesn't know what to return. You need to add a statement that returns some value. I'm not sure what the code is supposed to do, so I don't know whether that should be an error code of some kind or something else. But yes - you will need to change the body of the method, there's no way around that.


Ping & DNS - my free Android networking tools app
Sara Lyons
Greenhorn

Joined: Dec 05, 2012
Posts: 14
Ulf Dittmer wrote:The method is declared to return an "int" - so it must return an int under all circumstances. As it is, it may reach the end of the for loop - where there is no return statement, so the JVM doesn't know what to return. You need to add a statement that returns some value. I'm not sure what the code is supposed to do, so I don't know whether that should be an error code of some kind or something else. But yes - you will need to change the body of the method, there's no way around that.

Well I was looking at some past exam paper questions and one of the questions says what is the value of the following method calls: strange("one two three four") and strange("is there a bee in there") for this code:
public int strange(String sentence)
{
boolean isSpace = false;
int count = 0;
for (int i = 1; i < sentence. length ( ) ; i++)
{
isSpace = (sentence.charAt(i) == ' ');
if(isSpace&&sentence.charAt(i-1) == 'e')
{
count = count + 1;
return count;
}
}
So I was trying to compile it one my computer but not really sure what code goes before and after it since I don't have a clue about that int method
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 140

Sara Lyons wrote:

I guess you just need to put the return statement somewhere else


SCJP, SCWCD, SCJD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whats wrong with this code? public int method
 
Similar Threads
Help with english to morse code program, I have no idea what I'm doing
why does this code work
3 lines i dont understand
if inside for method wont work?!? why?
String index out of range