Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > missing return statement compile error

Reply
Thread Tools

missing return statement compile error

 
 
Matt
Guest
Posts: n/a
 
      07-17-2004
In case1, it has the "missing return statement" compile error. But if
I do case 2 or case 3. Then no more compile errors.

What's the rationale? please advise. thanks!!

//case 1
public String returnTest(String s)
{
//compile error: missing return statement
if (s.equals("test"))
return "test";
}

//case 2
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
else
return "non-test";
}

//case 3
public String returnTest(String s)
{
if (s.equals("test"))
return "test";
return "non-test";
}
 
Reply With Quote
 
 
 
 
Woebegone
Guest
Posts: n/a
 
      07-17-2004
"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> In case1, it has the "missing return statement" compile error. But if
> I do case 2 or case 3. Then no more compile errors.
>
> What's the rationale? please advise. thanks!!
>
> //case 1
> public String returnTest(String s)
> {
> //compile error: missing return statement
> if (s.equals("test"))
> return "test";
> }
>
> //case 2
> public String returnTest(String s)
> {
> if (s.equals("test"))
> return "test";
> else
> return "non-test";
> }
>
> //case 3
> public String returnTest(String s)
> {
> if (s.equals("test"))
> return "test";
> return "non-test";
> }


In case (1), no return statement exists for the case !s.equals("test"), so
not all potential paths are covered. As written, the other two cases are
equivalent. The rationale would be something along the lines of "a method
declared to return a value of type T must return a value of type T."

HTH,
Sean.


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-17-2004
On 16 Jul 2004 21:23:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Matt) wrote or
quoted :

>//case 1
>public String returnTest(String s)
>{
> //compile error: missing return statement
> if (s.equals("test"))
> return "test";
>}


what value is it supposed to return if s is not equal test? You have
not specified.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Andrea Spinelli
Guest
Posts: n/a
 
      07-21-2004
(E-Mail Removed) (Matt) wrote in
news:(E-Mail Removed) om:

> public String returnTest(String s)
> {
> //compile error: missing return statement
> if (s.equals("test"))
> return "test";
> }


AClass a = new AClass();
String x = a.returnTest("foo");
// in your opinion, what the value of x should be?


> public String returnTest(String s)
> {
> if (s.equals("test"))
> return "test";
> else
> return "non-test";
> }

String y = a.returnTest("foo");
// now y is "non-test"

> public String returnTest(String s)
> {
> if (s.equals("test"))
> return "test";
> return "non-test";
> }

String y = a.returnTest("foo");
// now y is "non-test"

Do you see the difference now???

If you don't, you are up **** creek
[from _The Blues Brothers_]

Andrea

--
Andrea Spinelli - IT&T srl (E-Mail Removed)
Via Sigismondi, 40 - 24018 Villa d'Alme' (BG)
tel: +39+035636029 - fax: +39+035638129
http://www.imteam.it/
 
Reply With Quote
 
Shishir19 Shishir19 is offline
Junior Member
Join Date: Mar 2007
Posts: 1
 
      03-19-2007
hello everyone. I m new to java and specially to EJB.was working on a piece of code but i m getting the same missing return statement error.can anyone please help


public String ins_Deposit(String userid,int amount,String branch,String bank) throws SQLException
{
Statement ps = null;
try
{
ps = conn.createStatement();
String query="INSERT INTO BANK_TRANSACT VALUES('"+userid+"',sys_date,"+amount+",'D','"+bra nch+"','"+bank+"')";
int result = ps.executeUpdate(query);
if(result>0)
return "Transaction successfully completed,amount will be credited to your account within 24hrs. ";
else
return "Bank timings are between 8 AM to 8 PM IST,Record not inserted";
}
catch(SQLException e)
{
System.out.println("Unable to get Data: " + e);
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"return delete (new int)" compile but "return delete (new X X C++ 4 07-19-2010 05:47 PM
Java - missing return statement.. sweety5236 Java 0 04-09-2008 10:23 PM
missing return statement on non void functions; just a warning? macracan C++ 3 02-08-2008 05:12 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM



Advertisments