Hi Everyone, I've been working on creating this code for a project I've been working on but I can't seem to get it to work. I'm creating a service class that I can then pass to a client class that will read a list of cities that ends in a "*" and then for each city read a list of tempratures that ends with a "0". It then has to calculate the average temprature, highest and lowest, and then output them along with any tempratures over 100. I've talked to my professor and she told me that the solution is a nested while loop, and that the prompts for the final program should be in a seprate client class.

public class WeatherInput
{



 private static  double tempAverage = 0;
 private static  double tempHigh = 0;
 private static  double tempLow = 0;
 private static  double tempHundred = 0;
 public static String newCity = new String();
 public static String city = new String();





            public static void tempCalc(double temp)
            {
                while(city != *)
                {   

                city = newCity;
            }
                    while(temp != 0)
                    {

                    tempAverage = (temp + temp)/2;

                    if(tempHigh > temp)

                    tempHigh = temp;


                    if(tempLow < temp)

                    tempLow = temp;

                if(temp > 100)

                    tempHundred = temp;

                    temp++;
                    }
                System.out.print(tempAverage);
                }
        }

1) In line number 19,* is a String,enclose * with double quotes.
2) Secondly use .equals instead of ==(in your case city !="*" must be replaced with !city.equals("*")) as == is used to compare two objects and equals to compare the content of thoe objects.

Edited 3 Years Ago by IIM

Thank you!
So something like this?
Is there anything else I'm going to need/should improve?

public class WeatherInput
{



 private static  double tempAverage = 0;
 private static  double tempHigh = 0;
 private static  double tempLow = 0;
 private static  double tempHundred = 0;
 public static String newCity = new String();
 public static String city = new String();





            public static void tempCalc(double temp)
            {
                while(!city.equals("*"))
                {   

                city = newCity;
            }
                    while(temp != 0)
                    {

                    tempAverage = (temp + temp)/2;

                    if(tempHigh > temp)

                    tempHigh = temp;


                    if(tempLow < temp)

                    tempLow = temp;

                if(temp > 100)

                    tempHundred = temp;

                    temp++;
                    }
                System.out.print(tempAverage);
                }
        }
This article has been dead for over six months. Start a new discussion instead.