Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Return Statements in If Statements
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
eklypze




PostPosted: Thu Mar 09, 2006 7:42 pm   Post subject: 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:

code:

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. Smile
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Thu Mar 09, 2006 8:46 pm   Post subject: (No subject)

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_




PostPosted: Thu Mar 09, 2006 8:48 pm   Post subject: (No subject)

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




PostPosted: Thu Mar 09, 2006 8:49 pm   Post subject: (No subject)

how about actually take the time to read his code before commenting?
Justin_




PostPosted: Thu Mar 09, 2006 8:50 pm   Post subject: (No subject)

I did, I'm just not as agile as you are.
wtd




PostPosted: Thu Mar 09, 2006 8:57 pm   Post subject: (No subject)

Justin_ wrote:
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_




PostPosted: Thu Mar 09, 2006 9:21 pm   Post subject: (No subject)

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




PostPosted: Thu Mar 09, 2006 9:31 pm   Post subject: (No subject)

I'm pretty sure any reasonable Java compiler is capable of recognizing that.
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Thu Mar 09, 2006 9:49 pm   Post subject: (No subject)

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




PostPosted: Fri Mar 10, 2006 10:18 am   Post subject: (No subject)

Sorry I forgot to add that I imported the Scanner class for input. Embarassed

I didn't make this mistake on the actual program, I just haven't done Java for a while and forgot to add:

Java:
import java.util.Scanner;

Scanner input = new Scanner(System.in);


And as for the userChoice.nextInt(), another mistake of mine. Embarassed

Java:

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. Very Happy
Andy




PostPosted: Fri Mar 10, 2006 11:36 am   Post subject: (No subject)

the return statements should work fine.. what compiler are you using
eklypze




PostPosted: Fri Mar 10, 2006 12:00 pm   Post subject: (No subject)

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.

Java:

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[card] + ".");
   
    if ((card == 0) || (card == 13)) // if player gets an ace
    {
      while (! validNumber)
      {
        System.out.println("Would you like this card to be a 1 or an 11?");
        card = input.nextInt();
       
        /* if user input is valid */
        if ((card == 1) || (card == 11))
        {
          /* if one was selected add 1 */
          if (card == 1)
          {
            validNumber = true;
           
            return cardValue[card-1];
          }
          /* if eleven was selected add 11 */
          else
          {
            validNumber = true;
                       
            return cardValue[card+2];
          }
        }
        /* if user input was invalid */
        else
        {
          System.out.println("Invalid value for ace.");
        }
      }
    }
    /* if ace was NOT selected */
    else
    {
      return cardValue[card];
    }
}
Andy




PostPosted: Fri Mar 10, 2006 12:48 pm   Post subject: (No subject)

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




PostPosted: Fri Mar 10, 2006 1:50 pm   Post subject: (No subject)

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. Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: