Results 1 to 11 of 11
  1. #1
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default Help with nested conditional expression

    Write an expression using nested conditional expression operators that will set the String variable heightDescription to “Tall” if the variable height is more than 6, to “Short” if height is less than 5, and to “Average” otherwise.

    Java Code:
    import java.util.Scanner;
    
    public class QUIZ6Q4
    {
    
        public static void main(String[] args)
        {
            Scanner scan = new Scanner (System.in);
            System.out.println("enter height as int in feet only");
            int height = scan.nextInt();
            String heightDescription = ( height < 5 && height == 6 && height > 6) ? "Short" : "Average" : "Tall"; // is this considered a nested conditional expression?
            System.out.println(heightDescription);
        }
    
    }
    my question:
    is it possible to put 3 statements in this ternary statement? i am getting a error. Syntax error on token ":".
    ( i use eclipse )

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The syntax for the ternary operator is:
    Java Code:
    condition ? execute if true : execute if false
    You have a third operand there, that's why it's producing a syntax error.
    Also, I would consider this:
    Java Code:
    if(condition1 && condition2)
    a nested statement, since it is equal to:
    Java Code:
    if(condition1) {
      if(condition2)
    }
    I don't know about your teacher though, I've seen some pretty strange things from my uni professors.
    Last edited by m00nchile; 10-17-2010 at 01:22 AM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    No it's not considered a nested conditional ... it's considered a syntax error! As you've found ;)

    Frankly nested conditionals may not be the most readable way of expressing this. But if the assignment requires it, I think it would look like:

    Java Code:
    if(someCondition) {
        if(someOtherCondition) {
            // do something with heightDescription
        } else { // someCondition is true, but someOtherCondition is false
            // do something else with heightDescription
        }
    } else { // someCondition is false
        // do some third thing with heightDescription
    }
    Last edited by pbrockway2; 10-17-2010 at 01:24 AM.

  4. #4
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    I don't want 3 if statements... because they would not work for the desired outcome.

    this is the question:

    Write an expression using nested conditional expression operators that will set the String variable heightDescription to “Tall” if the variable height is more than 6, to “Short” if height is less than 5, and to “Average” otherwise.

    I don't want this:

    if (height < 5)
    {
    System.out.println("Short");
    }
    if ( height = 5 || height = 6 )
    {
    System.out.println("average");
    }
    etc...

    i need something that satisfies the question.

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Well you could consider abominations along the lines of

    Java Code:
    heightDescription = cond1 ? (cond2 ? "too hot" : "just right!") : "too cold";

    (if "conditional expression operators" is a funny way about talking about ternary operators)

  6. #6
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Well you could consider abominations along the lines of

    Java Code:
    heightDescription = cond1 ? (cond2 ? "too hot" : "just right!") : "too cold";

    (if "conditional expression operators" is a funny way about talking about ternary operators)
    yeah i don't know how to make it work... i tried a bunch of different variations and none worked correctly. it would say short when should be tall and such.

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    it would say short when should be tall and such

    That's no problem, providing your conditions are sensible. Just have it print out "A", "B" and "C" then run it with some data to see what messages should go in the place of the letters.

    (I realise this is a *horrible* way of figuring out the logic of the ternary operator. And that's just what makes it appropriate in this case.)

  8. #8
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default okay i finally figured out the syntax...

    thanks for all the help... i figured it out!

    Java Code:
            Scanner scan = new Scanner (System.in);
            System.out.println("enter height as int in feet only");
            int height = scan.nextInt();
            String heightDescription = ( height < 5) ? "Short" : (height > 6) ? "Tall" : "Average";
            System.out.println(heightDescription);
    it outputs the correct height...

    Thanks again.

  9. #9
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    how do you mark solved or something along the lines of that? I seen people tell other people to make sure to mark "solved"????

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Thread tools above the first post.

    EDIT: beaten to the punch
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. conditional statement
    By blindfolded916 in forum New To Java
    Replies: 12
    Last Post: 07-11-2010, 10:09 AM
  2. Lucene as Conditional Evaluator / Indexer?
    By cuebei in forum Lucene
    Replies: 0
    Last Post: 01-11-2010, 08:36 PM
  3. JSP Help - Conditional Meta Tags
    By jakavan in forum New To Java
    Replies: 0
    Last Post: 12-08-2008, 07:34 PM
  4. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 01:38 PM
  5. JFrame Conditional Close
    By hemanthjava in forum AWT / Swing
    Replies: 10
    Last Post: 06-29-2008, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •