declare decision structure inside of a System.out.println
- by user2899249
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\JavaStuffj Speed
C:\Users\GlaDOS\Desktop\JavaStuffdel *.class
C:\Users\GlaDOS\Desktop\JavaStuffjavac 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\JavaStuffjava Speed
Error: Could not find or load main class Speed
Any help on why this is happening would be greatly appreciated.