Error of "Class has no return statement"
i can't figure out way the compiler say's the class has no return statement .....
Code :
import java.io.*;
class ConsoleInput { // open class
public static String readLine() { // open readLine
try { // open try
StringBuffer line = new StringBuffer();
int a = 0;
char b;
System.out.println("input here:");
BufferedInputStream buff = new BufferedInputStream(System.in);
while (a != -1) { // open while
a = buff.read();
b = (char) a;
line.append(b);
} // close while
String sLine = line.toString();
if (sLine.length() == 0) { // open if
sLine = "no string";
} // close if
buff.close();
return sLine;
} catch (IOException e) { // open catch close try
System.out.println(e.getMessage());
} // close catch
} // close readLine
} // close class
Re: way missing return statement???
Hello mdstrauss,
The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method.
Whenever your method is a variable type like String, the method must always return a String result.
This is untested but for example:
Code :
import java.io.*;
class ConsoleInput { // open class
public static String readLine() { // open readLine
try { // open try
StringBuffer line = new StringBuffer();
int a = 0;
char b;
System.out.println("input here:");
BufferedInputStream buff = new BufferedInputStream(System.in);
while (a != -1) { // open while
a = buff.read();
b = (char) a;
line.append(b);
} // close while
String sLine = line.toString();
if (sLine.length() == 0) { // open if
sLine = "no string";
} // close if
buff.close();
return sLine;
} catch (IOException e) { // open catch close try
System.out.println(e.getMessage());
} // close catch
[B]return readLine();[/B]
} // close readLine
} // close class
Or you can always make the return method void:
Code :
import java.io.*;
class ConsoleInput { // open class
public static [B]void [/B]readLine() { // open readLine
try { // open try
StringBuffer line = new StringBuffer();
int a = 0;
char b;
System.out.println("input here:");
BufferedInputStream buff = new BufferedInputStream(System.in);
while (a != -1) { // open while
a = buff.read();
b = (char) a;
line.append(b);
} // close while
String sLine = line.toString();
if (sLine.length() == 0) { // open if
sLine = "no string";
} // close if
buff.close();
//return sLine;
} catch (IOException e) { // open catch close try
System.out.println(e.getMessage());
} // close catch
} // close readLine
} // close class
Re: way missing return statement??? qestion on anser
thank you it works but i don't understand in method readLine i have all ready a return statement that returns string
return sLine;
way do i need the second return statement
return readLine();
in the readLine method ???
Re: way missing return statement???
In the case where the first return statement won't be reached like for instance in an if you still need to have a return.
Hope that makes sense.
// Json
Re: way missing return statement???
i don't get it way do i need two of the returns one before the catch and one after
way not put only one after the catch and way the second one is "return readLine()"
and not "return sLine" ?
i don't get way the catch block makes a deference ??
and i don't see where in my program there could be a hole with no return the if is to insure the is a return
no matter what....?????
thank you very much for the help
i am knew to java .....
Re: way missing return statement???
ok i understood that if my program catch's a exception then there would be no return but way dose the program no recognize my sLine string it is still in the method ?????
i checked all these and they compile
Code :
import java.io.*;
class ConsoleInput
{
public static String readLine()
{
try{
StringBuffer line = new StringBuffer();
int a = 0 ;
char b;
System.out.println("input here:");
BufferedInputStream buff = new BufferedInputStream(System.in);
while (a != -1 )
{
a = buff.read();
b = (char) a;
line.append(b);
}
string sLine = line.toString();
buff.close();
return sLine;
}catch (IOException e){
System.out.println(e.getMessage());
[B]return null;[/B]
}
}
}
or this
Code :
import java.io.*;
class ConsoleInput
{
[B]static String sLine;[/B]
public static String readLine()
{
try{
StringBuffer line = new StringBuffer();
int a = 0 ;
char b;
System.out.println("input here:");
BufferedInputStream buff = new BufferedInputStream(System.in);
while (a != -1 )
{
a = buff.read();
b = (char) a;
line.append(b);
}
sLine = line.toString();
if (sLine.length()==0){
sLine = "no string";
}
buff.close();
return sLine;
}catch (IOException e){
System.out.println(e.getMessage());
}
[B]return sLine;[/B]
}
}
Re: way missing return statement???
Well basically when you have a return type on a method you must make sure the method returns something unless it throws an exception so even if you have a try catch in there you have to make sure you return something if you catch an exception. You could of course put your return after the catch which will be alright.
There is a minor flaw in your code though, I see you are using a BufferedInputStream and you call close() in the try, but if an exception is thrown while you do buff.read() you will never reach the buff.close() and hence you will get a memory leak in your program every time you hit this method.
Try something like this (with reservation for any errors as I type this from the top of my head):
Code :
BufferedInputStream buff = null;
try {
buff = new BufferedInputStream(System.in);
// Do some stuff with the buffer here
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the buffer here, because this will always
if(buff != null) {
try {
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Json
Re: way missing return statement???
thank you very very much your write it dose not work it keeps open a strem even if my input is enter say what is a memory leak and well it happen no matter if i get a exception i'll check your advice latter because i have to go
again thank you very much