Hello,

I just started a computer science course and I'm having issues with one of the assignments. I think for the most part, I've got it done correctly (although, I'm a little shaky with the if/else if statements), but I'm having a syntax problem on lines 15 and 19 ("illegal start of expression").

I'd really appreciate if someone could take a look over and let me know what they think.

/**Program objective is to calculate the Wind Chill Index.
The program is meant to display error messages when the user
inputs a temperature not within the range of -58 to 41 and a WindSpeed
below 2*/

import java.util.Scanner;

public class Assignment2
{
public static void main(String[] args)
{

double outsideTemperature, windSpeed, windChillTemp;
double velocity = Math.pow(windSpeed, 0.16);
double windChillTemp = 35.74 + 0.6215 * outsideTemperature - 35.75 * velocity + 0.4275 * outsideTemperature * velocity;
Scanner input = new Scanner (System.in);

/**Program prompts user to enter value for "outsideTemperature*/
System.out.print("Enter the Temperature in Fahrenheit: ");
outsideTemperature = input.nextDouble();

/**If the outside temperature entered is less than -58 or greater than 41 the program will display an error message*/
if (outsideTemperature < -58 || > 41)
{
System.out.print("Please enter a temperature between -58F and 41F");
}
/** If the temperature entered is greater or equal to -58 and less than 41, the program will continue*/
else if (outsideTemperature >= -58 || <= 41)
/**Program prompts user to enter value for the Wind Speed*/
{
System.out.print("Enter the Wind Speed in miles per hour: ");
windSpeed = input.nextDouble();
}
/**If the wind speed entered is less than 2, the program will display an error message */
if (windSpeed < 2)
{
System.out.print("Please enter a Wind Speed greater than 2MPH");
}
/**If the wind speed is greater than 2 the program will continue*/
else if (windSpeed >= 2)
{
/**Program calculates Wind Chill Index and displays the result*/
System.out.print("The Wind Chill index is: " + "windChillTemp");
}
}
}

Thanks!

Edited by Intrevix: Added comments

4
Contributors
13
Replies
14
Views
7 Years
Discussion Span
Last Post by adams161
Featured Replies
• 1
Ezzaral 2,714   7 Years Ago

You only have a partial expression after your OR operator[CODE]if (outsideTemperature < -58 || > 41) [/CODE]You aren't comparing anything to 41 in that statement. If you want to check it against 'outsideTemperature' then you must state that explicitly. The compiler just sees that you have nothing on the left …

You only have a partial expression after your OR operator

if (outsideTemperature < -58 || > 41)

You aren't comparing anything to 41 in that statement. If you want to check it against 'outsideTemperature' then you must state that explicitly. The compiler just sees that you have nothing on the left hand side of the '>'.

Thanks. I should've realized that was the issue when windSpeed was giving me no trouble.
As soon as I fixed that, I got this error:

Assignment2.java:15: variable windSpeed might not have been initialized
double velocity = Math.pow(windSpeed, 0.16);
^
Assignment2.java:16: variable outsideTemperature might not have been initialized
double windChillTemp = 35.74 + 0.6215 * outsideTemperature - 35.75 * velocity + 0.4275 * outsideTemperature * velocity;

---
I'm confused, because I thought the compiler initialized variables one at a time from top to bottom. So, I figured that would be the right way to go?

Updated code:

/**Program objective is to calculate the Wind Chill Index.
The program is meant to display error messages when the user
inputs a temperature not within the range of -58 to 41 and a WindSpeed
below 2*/

import java.util.Scanner;

public class Assignment2
{
public static void main(String[] args)
{

double outsideTemperature, windSpeed;
double velocity = Math.pow(windSpeed, 0.16);
double windChillTemp = 35.74 + 0.6215 * outsideTemperature - 35.75 * velocity + 0.4275 * outsideTemperature * velocity;
Scanner input = new Scanner (System.in);

/**Program prompts user to enter value for "outsideTemperature*/
System.out.print("Enter the Temperature in Fahrenheit: ");
outsideTemperature = input.nextDouble();

/**If the outside temperature entered is less than -58 or greater than 41 the program will display an error message*/
if (outsideTemperature < -58 || outsideTemperature> 41)
{
System.out.print("Please enter a temperature between -58F and 41F");
}
/** If the temperature entered is greater or equal to -58 and less than 41, the program will continue*/
else if (outsideTemperature >= -58 || outsideTemperature<= 41)
/**Program prompts user to enter value for the Wind Speed*/
{
System.out.print("Enter the Wind Speed in miles per hour: ");
windSpeed = input.nextDouble();
}
/**If the wind speed entered is less than 2, the program will display an error message */
if (windSpeed < 2)
{
System.out.print("Please enter a Wind Speed greater than 2MPH");
}
/**If the wind speed is greater than 2 the program will continue*/
else if (windSpeed >= 2)
{
/**Program calculates Wind Chill Index and displays the result*/
System.out.print("The Wind Chill index is: " + "windChillTemp");
}
}
}

Edited by Intrevix: n/a

you have

double outsideTemperature, windSpeed;
double velocity = Math.pow(windSpeed, 0.16);

windspeed is declared and on the next line it's used to calculate velocity, but in declaring it, you didn't assign it any value, i.e. int a =5; would be a declaration and assignment at same time.

I would wait to assign velocity with that math pow method until after you have a windspeed. You can still declare it just dont assign it till you have all the parts of its assignment populated with values.

Mike

Hi Mike,

Thanks for the reply. I've looked over it a few times, but I can't figure out away around one of the instructions we were given: Our variables have to be defined at the beginning of the program. Could this be a mistake in the instructions or is there another way around it?

By variables being defined maybe it's meant declaration, not assignment. windspeed is declared, but not assigned till you get input. maybe velocity can be declared but not assigned.

Also its possible to do a default assignment, is double velocity = 15; suggesting on average velocity is 15 so that is default, and when you have more accurate information for example windspeed, recalculate it.

Mike

Also to look smart, when doing a default assignment, like double velocity =15; comment what you are doing i.e. // default velocity till more information is known

Alright, I thought this would work

/**Program objective is to calculate the Wind Chill Index.
The program is meant to display error messages when the user
inputs a temperature not within the range of -58 to 41 and a WindSpeed
below 2*/

import java.util.Scanner;

public class Assignment2
{
public static void main(String[] args)
{

double outsideTemperature, windSpeed;
double velocity;
double windChillTemp;
Scanner input = new Scanner (System.in);

/**Program prompts user to enter value for outsideTemperature*/
System.out.print("Enter the Temperature in Fahrenheit: ");
outsideTemperature = input.nextDouble();

/**If the outside temperature entered is less than -58 or greater than 41 the program will display an error message*/
if (outsideTemperature < -58 || outsideTemperature> 41)
{
System.out.print("Please enter a temperature between -58F and 41F");
}
/** If the temperature entered is greater or equal to -58 and less than 41, the program will continue*/
else if (outsideTemperature >= -58 || outsideTemperature<= 41)
/**Program prompts user to enter value for the Wind Speed*/
{
System.out.print("Enter the Wind Speed in miles per hour: ");
windSpeed = input.nextDouble();
}
/**If the wind speed entered is less than 2, the program will display an error message */
if (windSpeed < 2)
{
System.out.print("Please enter a Wind Speed greater than 2MPH");
}
/**If the wind speed is greater than 2 the program will continue*/
else if (windSpeed >= 2)
{
velocity = Math.pow(windSpeed, 0.16);
/**Program calculates Wind Chill Index and displays the result*/
windChillTemp= 35.74 + 0.6215 * outsideTemperature - 35.75 * velocity + 0.4275 * outsideTemperature * velocity;
System.out.print("The Wind Chill index is: " + "windChillTemp");
}
}
}

But I'm still getting this:

Assignment2.java:36: variable windSpeed might not have been initialized if (windSpeed < 2)

My thoughts are that "windSpeed" is initialized on line 33 windSpeed = input.nextDouble(); ... is it not?

The compiler is smart enough to realize that depending on the results of the if else statement directly above that line regarding temperature, you may or may not have initialized windspeed.

a way to fix it is to put the whole if else regarding windspeed within the else if statement

else if (outsideTemperature >= -58 || outsideTemperature<= 41)
{ // open curly bracket

// your existing code in this if statment goes here

// now copy the windspeed if else code since you only want it to operate in here where windspeed is assigned
}// your closing bracket

// code below here on windspeed was moved above.

Mike

that suggestion is only technically correct. You really should make sure all the variables you need assigned are assigned before you work with them. when you say

System.out.print("Please enter a temperature between -58F and 41F");

Maybe make sure they finally do that and then go on to work with windspeed. Its possible to have loops that keep asking for input till you get what you want. a nice feature is a way to quit as well like please enter a number between or enter -100 to quit.

Mike

Awesome. Everything works, except the flow of the program.
The program displays the correct information when I stay within the parameters. However, when I enter say "-100" for outsideTemperature, I get:

Please enter a temperature between -58F and 41F
Please enter a Wind Speed greater than 2MPH

How would I go about stopping it at the first line? Did I use an else if wrong?

Just saw your reply Mike, thanks.
If you wouldn't mind taking a once over the code and let me know if I've adhered to proper style and such that would be awesome and much appreciated.
I took your advice and just initiated velocity to zero.

/**Program objective is to calculate the Wind Chill Index.
The program is meant to display error messages when the user
inputs a temperature not within the range of -58 to 41 and a WindSpeed
below 2*/

import java.util.Scanner;

public class Assignment2
{
public static void main(String[] args)
{
/**Declare variables*/
double outsideTemperature, windChillTemp;
double windSpeed=0;
double velocity = 0;
Scanner input = new Scanner (System.in);

/**Program prompts user to enter value for outsideTemperature*/
System.out.print("Enter the Temperature in Fahrenheit: ");
outsideTemperature = input.nextDouble();

/**If the outside temperature entered is less than -58 or greater than 41 the program will display an error message*/
if (outsideTemperature < -58 || outsideTemperature> 41)
{
System.out.println("A temperature between -58F and 41F is required");
System.exit(0);

}
/** If the temperature entered is greater or equal to -58 and less than 41, the program will continue*/
else if (outsideTemperature >= -58 || outsideTemperature<= 41)
/**Program prompts user to enter value for the Wind Speed*/
{
System.out.print("Enter the Wind Speed in miles per hour: ");
windSpeed = input.nextDouble();
}
/**If the wind speed entered is less than 2, the program will display an error message */
if (windSpeed < 2)
{
System.out.print("A Wind Speed greater than 2MPH is required");
System.exit(0);
}
/**If the wind speed is greater than 2 the program will continue*/
else if (windSpeed >= 2)
{
velocity = Math.pow(windSpeed, 0.16);
/**Program calculates Wind Chill Index and displays the result*/
windChillTemp= 35.74 + 0.6215 * outsideTemperature - 35.75 * velocity + 0.4275 * outsideTemperature * velocity;
System.out.print("The Wind Chill index is: ");
System.out.println(windChillTemp);
}
}
}

That code should work. On an unrelated note though, instead of exiting the program once the user gives an incorrect input (System.exit(0)), you might want to consider using a do-while loop which loops until the user gives a satisfactory input.

One thing i notice is you prompt the user to enter Temperature and Wind Speed. You correctly verify you have good numbers and if not your choice is to exit. Maybe it would be helpful for the person using the program if when asked to enter a number, say for Temperature, they be told ahead of time that the hope is they enter a number between x and y whatever they are for the question.

Sort of like "Enter the Temperature (between -58F and 41F is required) in Fahrenheit:"

Another, fancier data input scheme is

outsideTemperature = 100; // to prompt it to loop on next line
while(outsideTemperature < -58 || outsideTemperature> 41)
{

System.out.print("Enter the Temperature in Fahrenheit or -100 to quit: ");

outsideTemperature = input.nextDouble();

if(outsideTemperature == -100)
{
System.exit(0);
}

if (outsideTemperature < -58 || outsideTemperature> 41)
{
// they see this before being asked again on next iteration of loop
// if their value was good they wont see this and will exit loop
System.out.println("A temperature between -58F and 41F is required");
}

}// end while, it will loop again if it didn't exit and temperature is out of range

edit-- saw above post. a do while would work here too, and might be stylistically neater than my trip of setting outsideTemperature to 100 to force the initial go of the loop.

Edited by adams161: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.