Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jacenta Kresky
Courses Plus Student 38 PointsIllegal start to an expression, if else program. Whats wrong?
I am a beginner and what I know is that the start of my code works until it hits the first if on line 29. Every single else if stats the problem as being an illegal start to an expression. In addition the next line stats ')' expected and I don't know how to fix that either. here is the code:
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor. */ package lights;
import java.util.Scanner; /** *
- @author Jacenta
- last modified
-
3/5/2016 */ public class Lights {
/**
-
@param args the command line arguments */ public static void main(String[] args) {
Scanner input = new Scanner (System.in);
Float n = 400f ; char color;
if(400 < = n < 445) { System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Violet");
}else if (445 < = n < 475){ System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Indgo"; }else if (475 < = n < 510){ System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Blue"; }else if (510 < = n < 570){ System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Green"; }else if (570 < = n < 590){ System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Yellow"; }else if (590 < = n < 650){ System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Orange"; }else if (650 < = n < 700){ System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Red";}
} }
-
4 Answers
jcorum
71,830 PointsThere were still a few issues. (1) you are missing the final two closing curly braces, one for the main method and one for the class, and (2) you have two other errors on each print line. Here's an example of what you have:
System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Violet";
I've put it on one line so you can see the difference better. You are missing a + after the n in each print line, and a closing ) for the print() method. Fixed, the above line looks like this:
System.out.print("Wavelength beteen 400.0 and 700.0:" + n + "Wavelength color is Violet");
Here's the whole thing, with those errors corrected:
import java.util.Scanner; /** * * @author Jacenta * last modified * 3/5/2016 */
public class Lights {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
Float n = 400f ;
char color;
if(400 <= n && n < 445) {
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Violet");
} else if (445 <= n && n < 475){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Indgo");
} else if (475 <= n && n < 510){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Blue");
} else if (510 <= n && n < 570){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Green");
} else if (570 <= n && n < 590){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Yellow");
} else if (590 <= n && n < 650){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Orange");
} else if (650 <= n && n < 700){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Red");
}
}
}
jcorum
71,830 PointsYou can't separate the less than symbol from the equal symbol, and you can't write java like algebra. You have:
if(400 < = n < 445) { . . .
You must do something like this instead:
if (400 <= n && n < 445) { . . .
Jacenta Kresky
Courses Plus Student 38 PointsThank you, the program runs know but it will not print the color
jcorum
71,830 PointsJacinta, please include your fixed code.
Jacenta Kresky
Courses Plus Student 38 PointsThis is the part I fixed
if(400 <= n && n < 445) { System.out.print("Wavelength beteen 400.0 and 700.0:" + n "Wavelength color is Violet");
}else if (445 <= n && n < 475){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
"Wavelength color is Indgo";
}else if (475 <= n && n < 510){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
"Wavelength color is Blue";
}else if (510 <= n && n < 570){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
"Wavelength color is Green";
}else if (570 <= n && n < 590){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
"Wavelength color is Yellow";
}else if (590 <= n && n < 650){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
"Wavelength color is Orange";
}else if (650 <= n && n < 700){
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
"Wavelength color is Red";
This is what is runs
run: Wavelength beteen 400.0 and 700.0:540.0
jcorum
71,830 PointsHere's what I get when I run it with an input value of 400.0:
Wavelength beteen 400.0 and 700.0:400.0Wavelength color is Violet
There's a formatting problem here. I forgot to put spaces before and after the n in each print line. So, instead of doing this:
System.out.print("Wavelength beteen 400.0 and 700.0:" + n
+ "Wavelength color is Violet");
Do this instead:
System.out.print("Wavelength beteen 400.0 and 700.0: " + n
+ " Wavelength color is Violet");
Notice the space I've added after the : and before the W:
. . . 700.0: " + n + " Wa . . .
Hope that does it. Cheers.
Jacenta Kresky
Courses Plus Student 38 PointsJacenta Kresky
Courses Plus Student 38 PointsThank you