George Putnam
George Putnam

Reputation: 27

datatypes and arrays in java

Thoroughly confused on how to do this. What I want to do is to place the city with the lowest min, or max in the output. My understanding is you cannot throw a string in with another datatype in a method. How in the world can I match the name with the lowest temperature?

Lets say I want 3 cities:
I want to make the array 3 then:
Then I will add in the following cities, (Alanta, New York, Richmond)
The cities temperatures are (42.2, 98.8, -12.4)

Min is -12.4
Max is 98.8
That I have, how do I link Richmond's String that is stored in array[2] to temperature's double that is stored in array[2]? Any help is much appreciated.

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.Math;

public class Ex9
{
public static void main(String[] args)
{
    String message ="";
    double min = 0, max = 0, avg = 0;
    int counter = 1;

    int numberOfCities = Integer.parseInt(JOptionPane.showInputDialog(null, "How many cities would you like to enter?"));
    String[] nameOfCities = new String[numberOfCities];
    double[] temperatureOfCities = new double[numberOfCities];
    for (int i = 0; i < nameOfCities.length; i++)
    {

        nameOfCities[i] = JOptionPane.showInputDialog(null, "Please enter the name of  city " +counter+" :");
        temperatureOfCities[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the current temperature of the city " + counter +" :"));
        message += "City name " + nameOfCities[i] + ".\n"
        + "Temperature of city " + temperatureOfCities[i] + " is degrees\n";
        counter++;


    }//end numberOfCities loop

    if(
    JOptionPane.showMessageDialog(null, message + "\nThe average temperature is " +findAvg(temperatureOfCities)+ "\n[Name of city] has the lowest temperature, which is " + findMin(temperatureOfCities) + "\n[Name of city] has the highest temperature, which is " + findMax(temperatureOfCities));

}//end main

public static double findAvg(double[] temperatureOfCities)
{
    double sum =0;
    for(int i=0;i<temperatureOfCities.length;i++)
    {
        sum += temperatureOfCities[i];
    }
    sum = sum/temperatureOfCities.length;
    return sum;

}//end findAvg

public static double findMin(double[] temperatureOfCities)
{
    double min=0;
    for(int i =0; i <temperatureOfCities.length;i++)
    {
        if (temperatureOfCities[i] <= temperatureOfCities[0])
        {
            min = temperatureOfCities[i];
        }
    }//end for loop
    return min;
}//end findMin

public static double findMax(double[] temperatureOfCities)
{
    double max=0;
    for(int i =0; i <temperatureOfCities.length;i++)
    {
        if (temperatureOfCities[i] >= temperatureOfCities[0])
        {
            max = temperatureOfCities[i];
        }
    }//end for loop
return max;
}//end findMax


}//end program

Upvotes: 0

Views: 72

Answers (2)

Cody A. Ray
Cody A. Ray

Reputation: 6017

Change your findMin, findAvg, and findMax methods to return a composite Measurement object.

class Measurement {
  final double temperature;
  final String cityName;

  Measurment(String cityName, double temperature)
  {
    this.temperature = temperature;
    this.cityName = cityName;
  }
}

The updated methods could look something like this:

public static Measurement findMax(String[] nameOfCities, double[] temperatureOfCities) {
  double maxTemp=0;
  String maxName=null;
  for(int i =0; i <temperatureOfCities.length;i++)
  {
      if (temperatureOfCities[i] >= temperatureOfCities[0])
      {
          maxTemp = temperatureOfCities[i];
          maxName = nameOfCities[i];
      }
  } //end for loop
  return new Measurement(maxTemp, maxName);
}

Now you can use the results like this:

Measurement maxMeasurement = findMax(nameOfCities, temperatureOfCities);
System.out.println(maxMeasurement.cityName + "has a temperature of " + maxMeasurement.temperature);

Similar goes for findMin and findAvg.

Upvotes: 0

Patashu
Patashu

Reputation: 21773

Two main approaches here:

1) The procedural approach - just pass both arrays around instead of just one array. If they're kept synchronized there's no problem - just use the same index for both.

2) The object oriented approach - Define a class TemperatureReading with double temperature and string cityName. Then you can make a TemperatureReading[] array and pass it around, and the data is naturally associated.

Upvotes: 1

Related Questions