java - Exception in thread "main" line 37 -


i'm getting "exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 @ weatheranalysis.main(weatheranalysis.java:37)" im not sure wrong @ line 37, date[currentline][0] = (int)fulldate % 100;. trying use .txt file output weather patterns 1941 2013.

import java.io.file; import java.io.filenotfoundexception; import java.util.scanner;  public class weatheranalysis {      public static void main(string[] args)  throws filenotfoundexception {          int numberofdays = 0;         string station;         file f = new file("portlandweathercut.txt"); //import text file.         scanner input= new scanner(f);         input.nextline();         input.nextline();          while (input.hasnextline()) {  //assign number of days                      numberofdays++;             input.nextline();         }          double[][] date = new double[numberofdays][3];         double[] prcp = new double[numberofdays];         double[] snow = new double[numberofdays];         double[] snwd = new double[numberofdays];         double[] tmax = new double[numberofdays];         double[] tmin = new double[numberofdays];         int currentline = 0;         double fulldate = 0.0;          file g = new file("portlandweather.txt"); //import text file.         scanner weather= new scanner(g);         weather.nextline(); weather.nextline();          while(weather.hasnextline()) { //variables , converts inches.         station = weather.next();         fulldate = weather.nextdouble();         date[currentline][0] = (int)fulldate % 100;         date[currentline][1] = ((int)fulldate % 10000) / 100;         date[currentline][2] = (int)fulldate / 10000;         prcp[currentline] = weather.nextdouble()* .00393701;         snow[currentline] = weather.nextdouble()* 0.0393701;         snwd[currentline] = weather.nextdouble()* 0.0393701;         tmax[currentline] = (weather.nextdouble() / 10.0) * (9.0/5.0) + 32.0;         tmin[currentline] = (weather.nextdouble() / 10.0) * (9.0/5.0) + 32.0;         currentline++;         weather.nextline();     }     int indexpoint = 0;     int nextpoint = 1;     int endpoint = 0;     (int x = 1; x < date.length; x++){          //loop calculates array section averaged,         //uses method average them, prints table         {                 nextpoint++;                 endpoint++;         }         while(((int)date[nextpoint - 1][02] / 10) == ((int)date[nextpoint][1] / 10));          if ( nextpoint < date.length) {         system.out.printf("%4.0f's average max temp = %4.1f\taverage min   temp = %4.1f\n",                  date[indexpoint][02],arrayavg(tmax, indexpoint, endpoint),                     arrayavg(tmin, indexpoint, endpoint));             indexpoint = nextpoint;             } else                  system.out.println();          }     }     public static double arrayavg(double a[], int fromindex, int toindex) {         //averages values between entered parameters , returns it.         double sum = 0;         double divisor = 0;         (int = fromindex; < toindex; i++){             if (a[i] == 393.6616299) {                 divisor++;             } else {                 sum += a[i];                 divisor++;             }            }         double average = sum / divisor;         return average;     } } 

seems issue @ line 37. think

date[currentline][0] = (int)fulldate%100 

inside of while loop. loop continues above numberofdays date 2-dimensional array created with, therefore throws out of bout.

my first advise break down monster function smaller function (look single responsibility principle).

my second advise tdd (test driven development)

but if want quick , dirty debug, following @ beginning of while loop:

system.out.printf("current line = %d, weather has next line? = %b", currentline, weather.hasnextline) 

this show values of currentline before exception , shows value of boolean.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -