Note that there are some explanatory texts on larger screens.

plurals
  1. POdatatypes and arrays in java
    text
    copied!<p>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?<br><br> Lets say I want 3 cities: <br> I want to make the array 3 then:<br> Then I will add in the following cities, (Alanta, New York, Richmond)<br> The cities temperatures are (42.2, 98.8, -12.4)<br><br></p> <p>Min is -12.4<br> Max is 98.8<br> 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. </p> <pre><code>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 &lt; 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&lt;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 &lt;temperatureOfCities.length;i++) { if (temperatureOfCities[i] &lt;= 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 &lt;temperatureOfCities.length;i++) { if (temperatureOfCities[i] &gt;= temperatureOfCities[0]) { max = temperatureOfCities[i]; } }//end for loop return max; }//end findMax }//end program </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload