File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Quick question about returning integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick question about returning integers" Watch "Quick question about returning integers" New topic
Author

Quick question about returning integers

Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
Hello,

Could someone please tell me why I get an error saying that I need to make a return statement, when I already did?



Thank you!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

what will your code return if face does not equal Face.Three?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

And why is that a while statement instead of an if statement?
Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
fred rosenberger wrote:what will your code return if face does not equal Face.Three?

It would return an integer of 0, or at least that's what I need my program to do. I am trying to create a canasta card game, where the program calculates how much points you have based on what cards you have. For this method, I want to try to check the hand to see if there are any threes of diamond or heart.


[EDIT]: I think I know what you were trying to point out. My else return statement should be outside of the while statement. I think.


Matthew Brown wrote:And why is that a while statement instead of an if statement?

I am not sure. Don't they give the same results? Is it better to use nested if statements?



[EDIT]: I think I see why if statement would be more appropriate than the while statement. I should use the nested if statement?

[EDIT2]: Eclipse still tells me that I need to add a return statement, and I don't know why D:
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

OK, now: what do you return if the card is the 3 of Spades, for instance? You need to make sure that something is returned from every possible path through the function. There are a few different ways you can fix it.

(The while statement could work, but it's a loop construct - you use it when you might want to execute the body multiple times. If you don't, like in this case, if is better).
Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
Matthew Brown wrote:OK, now: what do you return if the card is the 3 of Spades, for instance? You need to make sure that something is returned from every possible path through the function. There are a few different ways you can fix it.

(The while statement could work, but it's a loop construct - you use it when you might want to execute the body multiple times. If you don't, like in this case, if is better).


I thought that whatever path that is not defined would be just ignored, but I guess not. How's this?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

Jeong Ryu wrote:
I thought that whatever path that is not defined would be just ignored, but I guess not. How's this?


re: paths ignored
Nope. You need a return statement for every possible execution path. The method declaration states "I will return an int", therefore, you have to return an int for every possible path through your code.

re: how's this
Well, my first thought is "what happened when you ran/tested it?". My next thought is that the compiler will still complain. What happens if your suit is not a Spades, Clubs, Diamonds, or Hearts?

I would also suggest you get in the habit of ALWAYS using curly braces, even if not strictly needed. I would put your "return 0;" on line 1 inside a pair of braces.
Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
fred rosenberger wrote:
Jeong Ryu wrote:
I thought that whatever path that is not defined would be just ignored, but I guess not. How's this?


re: paths ignored
Nope. You need a return statement for every possible execution path. The method declaration states "I will return an int", therefore, you have to return an int for every possible path through your code.

re: how's this
Well, my first thought is "what happened when you ran/tested it?". My next thought is that the compiler will still complain. What happens if your suit is not a Spades, Clubs, Diamonds, or Hearts?

I would also suggest you get in the habit of ALWAYS using curly braces, even if not strictly needed. I would but your "return 0;" on line 1 inside a pair of braces.


I got the same error, haha. I tried adding another else to account for if my suit is not a spades, clubs, diamonds, or hearts, and it got rid of the error. Is this the right code to check if there are any threes of diamonds or hearts in the hand dealt? Also, what do you mean by "I would but your "return 0"; on line 1 inside a pair of braces"?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

type...corrected. should have been "I would put your..." so don't do this:


always do this:

use whatever brace style you prefer, but always use braces, even if not needed.

Is this the right code to check if there are any threes of diamonds or hearts in the hand dealt?

again - what happened when you TESTED your code? That is the only way to know if it is right or not.

it may work...but man is it ugly. WHY do you need the "else if" on line 7?

I think you have succumbed to the "gode by guessing" method. you don't know what you need to do, so you guess. You get errors, so you guess what the fix is. That may or may not work, so you guess again... eventually you have a mess like this.

I would say what you really need to do is throw all this code away. Turn off your computer. get some paper and pencils. Start writing down what you need this method to do, in English, not java. pretend you are explaining it to a child. Then revise that. then revise it again. use smaller words each time you revise it.

Only when you've done all that should you write a line of code.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
fred rosenberger wrote: . . .
I would say what you really need to do is throw all this code away. . . .
And also look at a style guide, where you find you ought to return that sort of value differently anyway.
Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
Thanks for the replies everyone.

I am going to scrap my program and do it all over again. This time with proper planning.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick question about returning integers