declare decision structure inside of a System.out.println

Posted by user2899249 on Stack Overflow See other posts from Stack Overflow or by user2899249
Published on 2013-10-20T03:44:05Z Indexed on 2013/10/20 3:54 UTC
Read the original article Hit count: 144

So I am working on an assignment where I have to have a print method in a constructor that displays a distance. I also have to have three separate get methods depending on what the input is in the demo class. My question is that I am trying to write the print method to contain a decision structure based on which get is used.

    public void prt()
{
    DecimalFormat formatter = new DecimalFormat("#,##0.00");

    System.out.println("The time it takes the sound to travel " + distance +
                           " feet through air is " + if       (getSpeedInAir() > 0)
                                                       {
                                                          formatter.format(getSpeedInAir());
                                                       }
                                                     else if (getSpeedInWater() > 0)
                                                       {
                                                          formatter.format(getSpeedInWater());
                                                       }
                                                     else if (getSpeedInSteel() > 0)
                                                       {
                                                          formatter.format(getSpeedInSteel());
                                                       }
                                                     else
                                                       "error";) 
}

After trying to compile I am getting the following errors.

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\GlaDOS\Desktop\JavaStuff>j Speed

C:\Users\GlaDOS\Desktop\JavaStuff>del *.class

C:\Users\GlaDOS\Desktop\JavaStuff>javac Speed.java Speed.java:43: error: illegal start of expression " feet through air is " + if (getSpeedInAir() > 0)

 ^

Speed.java:43: error: ';' expected " feet through air is " + if (getSpeedInAir() > 0)

   ^

Speed.java:43: error: not a statement " feet through air is " + if (getSpeedInAir() > 0)

                     ^

Speed.java:43: error: ';' expected " feet through air is " + if (getSpeedInAir() > 0)

                        ^

Speed.java:47: error: 'else' without 'if'

                             else if (getSpeedInWater() > 0)

                             ^

Speed.java:56: error: not a statement

                               "error";)

                               ^

Speed.java:56: error: illegal start of expression

                               "error";)

                                       ^

7 errors

C:\Users\GlaDOS\Desktop\JavaStuff>java Speed Error: Could not find or load main class Speed

Any help on why this is happening would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about if-statement