cuando ejecuto este programa recibo el mensaje de error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at WeatherGenerator.main(WeatherGenerator.java:188
Este programa se detiene en la linea 188
double longitude = Double.parseDouble(args[0]);
Given a location (longitude, latitude) in the USA and a month of the year, the method returns the forecast for the month based on the drywet and wetwet transition probabilities tables.
month will be a value between 2 and 13: 2 corresponds to January, 3 corresponds to February and so on. These are the column indexes of each month in the transition probabilities tables. The first day of the month has a 50% chance to be a wet day, 0-0.49 (wet), 0.50-0.99 (dry) Use StdRandom.uniform() to generate a real number uniformly in [0,1)
public class WeatherGenerator {
static final int WET = 1; // Use this value for a wet day
static final int DRY = 2; // Use this value for a dry day
// Number of days in each month, January is index 0, February is index 1
static final int[] numberOfDaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static int[] oneMonthGenerator(double longitude, double latitude,
int month, double[][] drywet, double[][] wetwet) {
// WRITE YOUR CODE HERE
double random_number = 0;
int firstday;
int[] forecast = new int[numberOfDaysInMonth[month-1]];
if (StdRandom.uniform() <= 0.49) {
firstday = WET;
} else {
firstday = DRY;
}
forecast[0]= firstday;
double wet_to_wet = 0;
double dry_to_wet = 0;
for (int i = 0; i < drywet.length; i++) {
if (drywet[i][0] == longitude && drywet[i][1]== latitude) {
dry_to_wet = drywet[i][month+1];
break;
}
}
for (int i = 0; i < wetwet.length; i++) {
if (wetwet[i][0] == longitude && wetwet[i][1] == longitude) {
wet_to_wet = wetwet[i][month+1];
break;
}
}
for (int i =0; i < forecast.length - 1; i++) {
if (forecast[i]== WET) {
random_number = StdRandom.uniform();
if (random_number <= wet_to_wet) {
forecast[i+1] = WET;
}else{
forecast[i+1] = DRY;
}
}
else {
random_number = StdRandom.uniform();
if (random_number <= dry_to_wet) {
forecast[i + 1] = DRY;
} else {
forecast[i+1] = WET;
}
}
}
return forecast;
}
public static void main (String[] args) throws ArrayIndexOutOfBoundsException {
int numberOfRows = 4100; // Total number of locations
int numberOfColumns = 14; // Total number of 14 columns in file
// File format: longitude, latitude, 12 months of transition probabilities
// Allocate and populate arrays that hold the transition probabilities
double[][] drywet = new double[numberOfRows][numberOfColumns];
double[][] wetwet = new double[numberOfRows][numberOfColumns];
populateTransitionProbabilitiesArrays(drywet, wetwet, numberOfRows);
/*** WRITE YOUR CODE BELLOW THIS LINE. DO NOT erase any of the lines above. ***/
// Read command line inputs
double longitude = Double.parseDouble(args[0]);
double latitude = Double.parseDouble(args[1]);
int month = Integer.parseInt(args[2]);
int[] forecast = oneMonthGenerator(longitude, latitude, month, drywet, wetwet);
int drySpell = lengthOfLongestwetdrySpell(forecast, DRY);
int wetSpell = lengthOfLongestwetdrySpell(forecast, WET);
StdOut.println("There are " + forecast.length + " days in the forecast for month " + month);
StdOut.println(drySpell + " days of dry spell.");
for ( int i = 0; i < forecast.length; i++ ) {
// This is the ternary operator. (conditional) ? executed if true : executed if false
String weather = (forecast[i] == WET) ? "Wet" : "Dry";
StdOut.println("Day " + (i+1) + " is forecasted to be " + weather);
}
}
}