Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: If Statement Java Need help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If Statement Java Need help

    I keep getting an missing statement error when I try and compile this at the end. I am not sure what I am doing wrong, if I need to post all the code I have before this please let me know, but I get no error until I get to this method. The instructions are inside the comments beginning with STEP 3.




    Error I get is missing return statement and points to the last }



     
       1. /** 
       2.    * Accessor to get the face value of the card as a String.  Specifically, 
       3.    * this method will return to you a String with one of "Ace", "2", "3", ..., "10", "Jack", 
       4.    * "Queen", "King".  This method should return "NOT A CARD" if the face value is invalid. 
       5.    *  
       6.    * @return A String with the face value of the Card 
       7.    */  
       8.   public String getCardFaceValue()  
       9.   {  
      10.       //STEP 3: Write the Java statements necessary to determine which card is represented  
      11.       //      by the instance field faceValue.  Return the String that describes that card.  
      12.       //      If the value of faceValue is not valid (not one of the 13 legal face values,  
      13.       //      return the string "NOT A CARD".  
      14.       //  
      15.       //      HINT: You will need an if statement.  You will likely want to specifically  
      16.       //          use the optional else if part as well.  
      17.       //      HINT 2: In the conditions of your if statement, you can compare the value of  
      18.       //          faceValue to the constant that represents the Ace.  If those values are  
      19.       //          equal, have a return statement that returns "Ace".  You'll then want  
      20.       //          an else if with a condition that checks if the card is a King, and so forth.  
      21.       //      HINT 3: If you determine that the card is one of the numbered values, you will  
      22.       //          need to return the String version of it.  Recall that one way of turning  
      23.       //          a number into a String is to concatenate it with the empty String.  That is  
      24.       //          If n is a variable of type int and you want to return the String version of it,  
      25.       //          you can do: return "" + n;  
      26.         
      27.    if(faceValue == 1) {  
      28. return "Ace";  
      29.   
      30. else if(faceValue==2) {  
      31.     return "Two";  
      32.   }  
      33.   else if(faceValue==3) {  
      34.       return "Three";  
      35.   }  
      36.   else if(faceValue==4) {  
      37.       return "Four";   
      38.   }  
      39.     
      40.    else if(faceValue==5) {  
      41.       return "Five";   
      42.   }  
      43.    else if(faceValue==6) {  
      44.       return "Six";   
      45.   }  
      46.    else if(faceValue==7) {  
      47.       return "Seven";   
      48.   }  
      49.    else if(faceValue==8) {  
      50.       return "Eight";   
      51.   }  
      52.    else if(faceValue==9) {  
      53.       return "Nine";   
      54.   }  
      55.   
      56.    else if(faceValue==10) {  
      57.       return "Ten";   
      58.         
      59.   }  
      60.   
      61.    else if(faceValue==11) {  
      62.       return "Jack";   
      63.   }  
      64.    else if(faceValue==12) {  
      65.       return "Queen";   
      66.   }  
      67.    else if(faceValue==13) {  
      68.       return "King";   
      69.   }  
      70.   
      71.    else if(faceValue  >=13) {  
      72.       return "NOT A CARD";  
      73.   }


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: If Statement Java Need help

    You have a "{" which never closes on the first if statement on line 27.

    27.    if(faceValue == 1) {  
      28. return "Ace";  
      29.   }
      30. else if(faceValue==2) {  
      31.     return "Two";  
      32.   }

    I put a closing } on line 29, that should take care of your problem with braces. Also, since you didn't post the entire code, I'm not sure if you properly closed the method with a } so you may need to add one at the very end.

    And as for missing return, it's because java requires a return statement outside of an if statement if I remember correctly. So even if the return is never used, add one after the if statements, such as returning an error or something.

    Hope that helps!
    Last edited by Bill_H; October 24th, 2009 at 07:32 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If Statement Java Need help

    I actually do have that in there. I don't know why it didnt show up. This is the complete code. I am still getting the missing return statement error after step 3 for the if statements.


     
    public class PlayingCard
    {
        private int faceValue;
        private int suitValue;
     
        public static final int HEARTS = 0;
        public static final int DIAMONDS = 1;
        public static final int SPADES = 2;
        public static final int CLUBS = 3;
     
        public static final int ACE = 1;
        public static final int KING = 13;
        public static final int QUEEN = 12;
        public static final int JACK = 11;
     
        /**
         * Constructor for PlayingCard class.
         * Generates a random playing card (randomly selected face value and suit).
         */
        public PlayingCard()
        {
            generateNewRandomCard();
        }
     
        /**
         * Constructor for PlayingCard class.
         * 
         * @param face The face value for the card.  2 through 10 are used for the numbered cards.
         *              Pass one of the named constants as the value for this parameter for the
         *              Ace, King, Queen, or Jack.
         *              
         * @param suit The suit of the card.  Pass one of the named constants for the suit of the
         *              card.
         */
        public PlayingCard(int face, int suit)
        {
            faceValue = face;
            suitValue = suit;
        }
     
        /**
         * Generates a random card.  That is, this method can be called to select a
         * new random face value and suit.
         */
        public void generateNewRandomCard()
        {
            Random flip = new Random();
     
            // STEP 1: Complete the following statement so that a random "suit" is selected.
            //          Note that we have been given constants above that will be used to
            //          represent each of the suits.  You want to complete the following
            //          statement to select a random integer in the range 0 to 3.
            //          Recall from a previous assignment, that we can generate a random
            //          number between 0 and n by calling the nextInt method of the Random
            //          class.  For example, our Random object has been called flip above,
            //          so we can generate a random number between 0 and n with flip.nextInt(n+1).
            //          In other words, if we want a random number between 0 and 9 we would
            //          have flip.nextInt(10)  Only here, we want a random number between
            //          0 and 3.
     
            suitValue = flip.nextInt(4)      ;
     
            // STEP 2: Complete the following statement to generate a random face value for the
            //          Card.  Face values can be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,
            //          King.  We have been given constants above to represent the "named" face
            //          values.  Specifically, we are using 1 for the Ace, 11, 12, and 13 for 
            //          Jack, Queen, and King.  2 through 10 will just be the obvious 2 through 10.
            //
            //          To generate a Random face value we will need a random number between 
            //          1 and 13.  HINT: To get this, you can call nextInt, but you will need to
            //          add 1 to the result, just like you did with the Dice program.
     
            faceValue = flip.nextInt(12) + 1      ;
     
        }
     
     
        /**
         * Accessor to get the face value of the card as a String.  Specifically,
         * this method will return to you a String with one of "Ace", "2", "3", ..., "10", "Jack",
         * "Queen", "King".  This method should return "NOT A CARD" if the face value is invalid.
         * 
         * @return A String with the face value of the Card
         */
        public String getCardFaceValue()
        {
            //STEP 3: Write the Java statements necessary to determine which card is represented
            //      by the instance field faceValue.  Return the String that describes that card.
            //      If the value of faceValue is not valid (not one of the 13 legal face values,
            //      return the string "NOT A CARD".
            //
            //      HINT: You will need an if statement.  You will likely want to specifically
            //          use the optional else if part as well.
            //      HINT 2: In the conditions of your if statement, you can compare the value of
            //          faceValue to the constant that represents the Ace.  If those values are
            //          equal, have a return statement that returns "Ace".  You'll then want
            //          an else if with a condition that checks if the card is a King, and so forth.
            //      HINT 3: If you determine that the card is one of the numbered values, you will
            //          need to return the String version of it.  Recall that one way of turning
            //          a number into a String is to concatenate it with the empty String.  That is
            //          If n is a variable of type int and you want to return the String version of it,
            //          you can do: return "" + n;
     
         if(faceValue == 1) {
      return "Ace";
    }
      else if(faceValue==2) {
          return "Two";
        }
        else if(faceValue==3) {
            return "Three";
        }
        else if(faceValue==4) {
            return "Four"; 
        }
     
         else if(faceValue==5) {
            return "Five"; 
        }
         else if(faceValue==6) {
            return "Six"; 
        }
         else if(faceValue==7) {
            return "Seven"; 
        }
         else if(faceValue==8) {
            return "Eight"; 
        }
         else if(faceValue==9) {
            return "Nine"; 
        }
     
         else if(faceValue==10) {
            return "Ten";  
        }
     
         else if(faceValue==11) {
            return "Jack"; 
        }
         else if(faceValue==12) {
            return "Queen"; 
        }
         else if(faceValue==13) {
            return "King"; 
        }
     
         else if(faceValue  >=13) {
            return "NOT A CARD";
        }
     
    }
     
     
     
     
        /**
         * Accessor to get the suit of the card as a String.  Specifically,
         * this method will return to you a String with one of "Hearts", Spades", "Clubs", "Diamonds".
         * It will return "NOT A CARD" if the suit is not one of the legal suits.
         * 
         * @return A String representing the name of the suit of the card.
         */
        public String getCardSuit()
        {
            //STEP 4: Write an if statement (you will likely need to use the else if part) to compare
            //      the value of the instance field suitValue to each of the 4 possible choices (the
            //      constants defined at the top).  This if statement should determine which
            //      suit the card is from and should return the name of it as a String.
            //      If the value stored in suitValue is not one of the 4 legal values, return "NOT A CARD"
            //      (hint: this last case can be an else).
     
     
     
     
    }
     
        /**
         * Accessor to get the suit as an integer.
         * @return the suit as an integer between 0 and 3 that corresponds to the named constants for the suit
         */
        public int getCardSuitAsInt()
        {
            // DO NOT CHANGE THIS METHOD IN ANY WAY
            return suitValue;   
        }
     
        /**
         * Accessor to get the face value as an integer.
         * @return the face value as an integer between 1 and 13 that corresponds to the named constants for the face value
         */
        public int getCardFaceAsInt()
        {
             // DO NOT CHANGE THIS METHOD IN ANY WAY
            return faceValue;   
        }
    }

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: If Statement Java Need help

    Ok, right here I notice that you have the program return a king at value 13, but at 13 or greater it returns not a card. Shouldn't this be greater than 13 like:

         else if(faceValue==13) {
            return "King"; 
        }
     
         else if(faceValue  > 13) {
            return "NOT A CARD";
        }

    And have you tried replacing
    else if(faceValue  >=13) {
            return "NOT A CARD";
        }

    with just a return statement, since anything not equal to the last if of 13 would be the same:
    return "NOT A CARD"

    Basically, try replacing the last else if statement with just the return statement.

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If Statement Java Need help

    Wow Thanks bro that did it, also do you think you could help me with part 4, I am not 100% sure on how to approach that.

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: If Statement Java Need help

    Quote Originally Posted by Keno888 View Post
    Wow Thanks bro that did it, also do you think you could help me with part 4, I am not 100% sure on how to approach that.
    You're welcome!

    As for part 4, I'm not entirely certain how your program works since I'm not the one making it, and just reading it doesn't always immediately click into place, but I'm guessing you want the getCardSuit() method to return a string showing the correct suit which corresponds to the correct number assigned earlier in the program.

    I can't guarantee that this will work, but assuming my guess on how the method behaves is right, this should at least get you on the right track.

    public String getCardSuit()
        {
            //STEP 4: Write an if statement (you will likely need to use the else if part) to compare
            //      the value of the instance field suitValue to each of the 4 possible choices (the
            //      constants defined at the top).  This if statement should determine which
            //      suit the card is from and should return the name of it as a String.
            //      If the value stored in suitValue is not one of the 4 legal values, return "NOT A CARD"
            //      (hint: this last case can be an else).
     
           String suit = "NOT A CARD";
     
            if (suitValue == 0) {
                   suit = "HEARTS";
                   return suit;
            }
            else if (suitValue == 1) {
                   suit = "DIAMONDS";
                   return suit;
            }
            else if (suitValue == 2) {
                   suit = "SPADES";
                   return suit;
            }
            else if (suitValue == 3) {
                   suit = "CLUBS";
                   return suit;
            }
     
            return suit;
        }

    This initializes String suit to "NOT A CARD" so you can do the same thing that I told you before, use the "error" as the default return in case none of the if statements catch it. If a value is correct, it assigns the string a value based on what I saw as your constant instance variables.

    I hope this helps!

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  3. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  4. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM