help with java language?! useing Scanner + 2D array?

help with java language?! useing Scanner + 2D array?

Suppose that temperature measurements were made on 7 days of 2009 in each of 5 cities.
write a program that will read the city name followed by the temperature measurements of that city.
the city name and measurements should be stored into two arrays.
the program should find out the average temperature for each city.
Also you should find the average temperature of each day.

Tags: , , , , ,

Under Forum

1 Comment for help with java language?! useing Scanner + 2D array?

  • 1. SPB  |  November 30th, 2010 at 8:28 pm

    This is a start. Not sure if this is what you wanted.

    import javax.swing.JOptionPane;

    public class Temperature {
    public static void main(String[] args) {
    String []cities = new String[5];
    double [][]temps = new double[cities.length][7];

    // Fill the data arrays
    for (int i = 0; i < cities.length; i++) {
    String city = JOptionPane.showInputDialog(“Enter city name “);
    cities[i] = city;

    for (int j = 0; j < temps[0].length; j++) {
    String sTemp = JOptionPane.showInputDialog(“Temperature in ” + city + ” on day ” + (j+1) + “:”);
    temps[i][j] = Double.parseDouble(sTemp);
    }
    }

    // Calculate and print averages
    for (int i = 0; i < cities.length; i++) {
    String city = cities[i];
    double sum = 0;
    for (int j = 0; j < temps[0].length; j++) {
    sum += temps[i][j];
    }
    System.out.println(“City: ” + city + ” had an average temperature of ” + (sum/7.0));
    }
    }
    }

Leave a Comment for help with java language?! useing Scanner + 2D array?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories