
-----------------------------------
eklypze
Thu Mar 09, 2006 7:42 pm

Return Statements in If Statements
-----------------------------------
I am currently having trouble in Java because I want a method to return a certain value depending on what the user chooses. For example:


public int drawPlayerCard() {
   int usrChoice;

   System.out.println("Would you like to return a 2? (1: yes - 2: no)");
   usrChoice.nextInt();

   if (usrChoice == 1)
   {
      return 2;
   } else {
      return 1;
   }

}


The problem is that this code says that it is missing a return statement. Is there anything I can do about this?

Sorry if this is bad coding style or anything, I just made it up quickly so you can understand my question. Thanks in advance. :)

-----------------------------------
Andy
Thu Mar 09, 2006 8:46 pm


-----------------------------------
ok yea, that's compeltely off... your usrChoice is an integer, not an object, you cant call a method nextInt() from it, which class are you using for input

-----------------------------------
Justin_
Thu Mar 09, 2006 8:48 pm


-----------------------------------
Incase you don't know about syntax highlighting: use [*syntax*="java"][/syntax] (minus the asterisks) instead of [*code*][/code]

secondly I think, though I'm not entirely sure, that it's because you need a return statement default, incase the if's fall through.  

so at the end of your if statement just put:

 return 0;

-----------------------------------
Andy
Thu Mar 09, 2006 8:49 pm


-----------------------------------
how about actually take the time to read his code before commenting?

-----------------------------------
Justin_
Thu Mar 09, 2006 8:50 pm


-----------------------------------
I did, I'm just not as agile as you are.

-----------------------------------
wtd
Thu Mar 09, 2006 8:57 pm


-----------------------------------
secondly I think, though I'm not entirely sure, that it's because you need a return statement default, incase the if's fall through. 

There is no possible way that with an "else" clause, that a "return" is not encountered.

-----------------------------------
Justin_
Thu Mar 09, 2006 9:21 pm


-----------------------------------
I know, but does the compiler necessarily "know" that?  That would mean the developers of Java would have had to have programmed the compiler to recognize the fact that else statements do not allow the if to fall through.  And since I haven't seen the source of the compiler I wouldn't be able to logically figure that out. 

It was just a guess, I said I wasn't sure.

-----------------------------------
wtd
Thu Mar 09, 2006 9:31 pm


-----------------------------------
I'm pretty sure any reasonable Java compiler is capable of recognizing that.

-----------------------------------
Justin_
Thu Mar 09, 2006 9:49 pm


-----------------------------------
Yeah, so I gather.  Thanks, I'm glad to know that.  You see, not just the owner of the thread gets to learn new things  :wink:

-----------------------------------
eklypze
Fri Mar 10, 2006 10:18 am


-----------------------------------
Sorry I forgot to add that I imported the Scanner class for input.  :oops: 

I didn't make this mistake on the actual program, I just haven't done Java for a while and forgot to add:

import java.util.Scanner;

Scanner input = new Scanner(System.in);


And as for the userChoice.nextInt(), another mistake of mine.  :oops: 


usrChoice = input.nextInt();


My main question was about the return if statements though. This isn't my actual program so please just worry about the if statement. :D

-----------------------------------
Andy
Fri Mar 10, 2006 11:36 am


-----------------------------------
the return statements should work fine.. what compiler are you using

-----------------------------------
eklypze
Fri Mar 10, 2006 12:00 pm


-----------------------------------
I have tried both Dr Java and JCreator, and both gave me the error "missing return statement". Maybe it would help if I gave the actual code.


public int drawPlayerCard()
  {
    // **************Initializing**************
    int card;
    boolean validNumber = false; // validate proper value for ace
    
    /* DRAW CARD: Player */
    card = (Math.abs(randomCard.nextInt()) % 13); // generate random card
    
    System.out.println("You have drawn: " + cardName

-----------------------------------
Andy
Fri Mar 10, 2006 12:48 pm


-----------------------------------
oic, umm question, what is the point of validNumber? 
you dont need it at all. change it to a wile(true) and your code should compile fine. the way you have it,  the only way to exit the loop is to hit the validNumber = true; statement, by right after, you return a value, so that statemend does absolutely nothing.

-----------------------------------
eklypze
Fri Mar 10, 2006 1:50 pm


-----------------------------------
validNumber is supposed to make sure that the user entered a correct value for Ace (1 or 11). If they haven't then the loop will repeat again and let the user input another value.

Your suggestion works as well though, thanks for the tip. It now compiles fine. :)
