LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2006, 04:59 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Rep: Reputation: 30
java +missing return statement? no it isnt


hello all im trying to make this statement but it says that im missing a return statement when im clearly not:


Code:
private boolean checkLogin( String user, String passwd ) {
   
        if( user.equalsIgnoreCase( "cookie" /* replace with server vars here */ ) ) {
           if( passwd.equals( "passWord" ) ) {
            return true;
           }
        } else {
            return false;
        }
    }
Code:
client/frm_logon.java [194:1] missing return statement
    }
    ^
1 error
Errors compiling frm_logon.
 
Old 04-21-2006, 05:27 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Yes you are; if the first "if" statement is true and the second "if" statement is false, it doesn't return anything.
 
Old 04-21-2006, 05:33 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
how could i modify that code to correct it?
 
Old 04-21-2006, 05:38 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
You could add an "else" clause to the inner if statement and then return something in that. I mean, it all depends on how exactly you wanted it to work.
 
Old 04-21-2006, 05:43 AM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
cheers for the quick reply i think ive got it seems to work anyway this is what ive done:

Code:
    private boolean checkLogin( String user, String passwd ) {
   
        if( user.equalsIgnoreCase( "cookie" /* replace with server vars here */ ) ) {
           if( passwd.equals( "passWord" ) ) {
            return true;
           }
        }
            return false;
    }
i thought that if the statement was true then it would run the scope of the function as well as the scope of the if so the return value would always be false, but i was wrong it seems to exit the function once the value has been set.
 
Old 04-21-2006, 05:51 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
I don't know java, but it might even be that it complains about the fact that there is no return before the last closing curly brace.

As said by spooon, it depends how you want to write your code. I would 'catch' all situations that should return true and let them return true.
When the program gets at the end of the function, it will then return false.

Code:
int myfunction(int a, int b)
{
    if(a==3)
    {
        return true;
    }
    else
    {
        if(a==5)
        {
            return true;
        }
        else
        {
            if(b==7)
            {
                return true;
            }
        }
    }

    // when we get her, it's false
    return false;
}
BTW, we have a programming forum
 
Old 04-21-2006, 09:38 AM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-21-2006, 01:32 PM   #8
debasish_5849
Member
 
Registered: Jan 2006
Location: India
Distribution: fedora core 5
Posts: 42

Rep: Reputation: 15
Quote:
Originally Posted by Wim Sturkenboom
I don't know java, but it might even be that it complains about the fact that there is no return before the last closing curly brace.[/code]
programming forum
No its not, it just requires a return statement under every condition. The java compiler, unlike the c compiler, can check for that.
 
Old 04-21-2006, 01:41 PM   #9
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by debasish_5849
The java compiler, unlike the c compiler, can check for that.
I didn't know there was only one compiler implementation for each of these two languages
From my experience, gcc is in fact able to check for missing return statements.
 
Old 04-21-2006, 08:57 PM   #10
debasish_5849
Member
 
Registered: Jan 2006
Location: India
Distribution: fedora core 5
Posts: 42

Rep: Reputation: 15
Quote:
Originally Posted by addy86
I didn't know there was only one compiler implementation for each of these two languages
From my experience, gcc is in fact able to check for missing return statements.
Right! Sorry, I meant to say that some of the C compilers can't and a compiler does not require to do this to be called a C compiler. But there is only one java compiler in the market ( and don't tell me that gcj is the other, cos what gcj does is to ape the original java compiler. There is no standard. A java compiler is what sun wants it to be.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault on return statement crosseyedalien Programming 9 06-17-2005 12:23 PM
return statement in functions pantera Programming 2 12-06-2004 06:21 PM
value is lost from a return statement! what?? ludeKing Programming 3 05-30-2004 08:32 PM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 11:00 AM
WTF? isnt linux free...how come lindows isnt? Cycopath81090 Linux - Newbie 11 08-22-2003 08:19 PM


All times are GMT -5. The time now is 12:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration