java - ArrayList Index Error -


i trying finish project uses graphical user interface search through data base of geysers in us. keep getting error 1 of array lists. include data program is 10,000 entries long. appreciated. being done in bluej

error : java.lang.indexoutofboundsexception: index: 0, size: 0

database code:

import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.*; import java.util.arraylist; import java.util.scanner;  public class geyserdatabase { private arraylist < geyser > geysers; private arraylist < eruption > eruptions;  public geyserdatabase() {      geysers = new arraylist < geyser >();     eruptions = new arraylist < eruption >(); }  public void readgeyserdata(string filename){      try{           file f = new file(filename);         scanner sc = new scanner(f);         string text;          // keep reading long there more data         while(sc.hasnext()) {             text = sc.nextline();              eruption e = new eruption(text);             eruptions.add(e);          }         sc.close();     }     catch(ioexception e) {         system.out.println("failed read data file: " + filename);     }     creategeyserlist(); }  public void adderuption (eruption e){     eruptions.add(e); }  public arraylist <eruption> geteruptionlist(){      return eruptions;  }  public arraylist <geyser> getgeyserlist(){      return geysers; }  public int getnumeruptions() {     return eruptions.size(); }  public int getnumeruptions(int m,int d,int y) {     int count = 0;     ( eruption e : eruptions) {         if (e.getmonth()==m && e.getday()==d && e.getyear()==y) {             count++;         }     }     return count; }  public eruption getlatenighteruption() {     eruption latesteruption = eruptions.get(0);     int latesthour   = latesteruption.gethour();     int latestminute = latesteruption.getminute();     ( eruption e: eruptions ) {         if (e.gethour() > latesthour || (e.gethour()==latesthour && e.getminute()>latestminute)) {             latesteruption = e;             latesthour = e.gethour();             latestminute = e.getminute();         }     }     return latesteruption; }  public arraylist < eruption > geteruptions(string geyser) {     arraylist < eruption > result = new arraylist < eruption > ();     ( eruption e: eruptions ) {         if (e.getgeysername().startswith(geyser)) {             result.add(e);         }     }     return result; }  private void creategeyserlist(){     arraylist<string>namelist = new arraylist<string>();     // create temporary list of unique geyser names     for(eruption e:eruptions){         if(!namelist.contains(e.getgeysername())){             namelist.add(e.getgeysername());         }     }      // create list of geysers     arraylist<geyser>geyserlist = new arraylist<geyser>();     for(string s:namelist){         geyser g = new geyser(s);          // count number of eruptions current geyser name         for(eruption e:eruptions){             if(e.getgeysername().equals(g.getname()))                 g.increment();         }         geyserlist.add(g);     } }  public int getmostgeysers(){      return geysers.size();  }  public geyser findmostactivegeyser(){      geyser mosteruptions = geysers.get(0);     int mostactive = mosteruptions.getnumeruptions();     for( geyser g : geysers){          if(g.getnumeruptions() > mostactive){             mosteruptions =g;             mostactive = g.getnumeruptions();          }      }     return mosteruptions; }  public geyser findleastactivegeyser() {      geyser leastactivegeyser = geysers.get(0);     int leastactive = leastactivegeyser.getnumeruptions();     for(geyser g: geysers)     {         if(g.getnumeruptions() < leastactive)         {             leastactivegeyser = g;             leastactive = g.getnumeruptions();         }     }     return leastactivegeyser; }  public string finddaywithmosteruptions(int y){      int daymost = 1;     int monthmost = 1;     int maxsofar = getnumeruptions(1,1,y);     for(int m = 1; m<=12; m++){          for(int d = 1; d<=31;d++){             int eruptionsday = getnumeruptions(m,d,y);              if(eruptionsday>maxsofar){                 daymost = d;                 monthmost = m;                 maxsofar=eruptionsday;             }          }      }     return monthmost + "/" + daymost + "/" + y + "eruptions: " + maxsofar; }  public static void main(string args[]){     geyserdatabase gdb = new geyserdatabase();      }    } 

gui code:

import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import java.io.*; import java.text.*;  /***********************************************************************  * gui front end yellowstone geyser database   *   * @author scott grissom  * @version august 1, 2016     public class geysergui extends jframe implements actionlistener{  /** results box */ private jtextarea resultsarea;  private geyserdatabase db;  /**jbuttons */ private jbutton findlatenighteruption; private jbutton findamountoferuptionsondate; private jbutton findmaxeruptionsinyear; private jbutton findgeyserbyname;  private jbutton findmostactivegeyser; private jbutton findleastactivegeyser; private jbutton getgeyserlist;  private jtextfield month; private jtextfield day; private jtextfield year; private jtextfield geyser;  /** menu items */ private jmenubar menus; private jmenu filemenu; private jmenu reportsmenu; private jmenuitem quititem; private jmenuitem openitem;  private jmenuitem countitem; private jmenuitem geyseritem;  /********************************************************************* main method  *********************************************************************/ public static void main(string arg[]){     geysergui gui = new geysergui();     gui.setdefaultcloseoperation(jframe.exit_on_close);     gui.settitle("yellowstone geysers");     gui.pack();     gui.setvisible(true);  }  /********************************************************************* constructor - instantiates , displays of gui commponents  *********************************************************************/ public geysergui(){     db = new geyserdatabase();     // fix me: following line should removed     db.readgeyserdata("geyserdata.txt");      // create gridbag layout     setlayout(new gridbaglayout());     gridbagconstraints position = new gridbagconstraints();      // create results text area (5 x 10 cells)     resultsarea = new jtextarea(20,40);     jscrollpane scrollpane = new jscrollpane(resultsarea);     position.gridx = 0;     position.gridy = 0;     position.gridheight = 10;     position.gridwidth = 5;        position.insets =  new insets(20,20,0,0);            add(scrollpane, position);        /*******************************************************/     position = new gridbagconstraints();     position.insets =  new insets(0,20,0,0);        position.gridx = 0;     position.gridy = 10;       position.gridwidth = 1;     position.gridheight = 1;       position.anchor = gridbagconstraints.line_start;     add(new jlabel("month"), position);      position = new gridbagconstraints();     position.gridx = 0;     position.gridy = 11;      position.gridwidth = 1;     position.gridheight = 1;     position.anchor = gridbagconstraints.line_start;     position.insets =  new insets(0,20,0,0);       month = new jtextfield(2);       add(month, position);      /*************************************************************/     position = new gridbagconstraints();     position.insets =  new insets(0,20,0,0);        position.gridx = 1;     position.gridy = 10;       position.gridwidth = 1;     position.gridheight = 1;       position.anchor = gridbagconstraints.line_start;     add(new jlabel("day"), position);      position = new gridbagconstraints();     position.gridx = 1;     position.gridy = 11;      position.gridwidth = 1;     position.gridheight = 1;     position.anchor = gridbagconstraints.line_start;     position.insets =  new insets(0,20,0,0);       day = new jtextfield(2);       add(day, position);      /**********************************************************/      position = new gridbagconstraints();     position.insets =  new insets(0,20,0,0);        position.gridx = 2;     position.gridy = 10;       position.gridwidth = 1;     position.gridheight = 1;       position.anchor = gridbagconstraints.line_start;     add(new jlabel("year"), position);      position = new gridbagconstraints();     position.gridx = 2;     position.gridy = 11;      position.gridwidth = 1;     position.gridheight = 1;     position.anchor = gridbagconstraints.line_start;     position.insets =  new insets(0,20,0,0);       year = new jtextfield(4);       add(year, position);      /**********************************************************/      position = new gridbagconstraints();     position.insets =  new insets(0,20,0,0);        position.gridx = 3;     position.gridy = 10;       position.gridwidth = 1;     position.gridheight = 1;       position.anchor = gridbagconstraints.line_start;     add(new jlabel("geyser"), position);      position = new gridbagconstraints();     position.gridx = 3;     position.gridy = 11;      position.gridwidth = 1;     position.gridheight = 1;     position.anchor = gridbagconstraints.line_start;     position.insets =  new insets(0,20,0,0);       geyser = new jtextfield(12);       add(geyser, position);       /*********************************************************/     position = new gridbagconstraints();     position.insets =  new insets(30,5,5,5);        position.gridx = 6;     position.gridy = 0;       position.gridwidth = 1;     position.gridheight = 1;      add(new jlabel("eruptions"), position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 1;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);       findlatenighteruption = new jbutton("late night eruption");      findlatenighteruption.addactionlistener(this);     add(findlatenighteruption, position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 2;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);      findamountoferuptionsondate = new jbutton("# on date");      findamountoferuptionsondate.addactionlistener(this);     add(findamountoferuptionsondate, position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 3;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);       findmaxeruptionsinyear = new jbutton("max eruptions in year");      findmaxeruptionsinyear.addactionlistener(this);     add(findmaxeruptionsinyear, position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 4;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);      findgeyserbyname = new jbutton("by name");      findgeyserbyname.addactionlistener(this);     add(findgeyserbyname, position);       position = new gridbagconstraints();     position.insets =  new insets(30,5,5,5);        position.gridx = 6;     position.gridy = 5;       position.gridwidth = 1;     position.gridheight = 1;      add(new jlabel("geysers"), position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 6;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);      findmostactivegeyser = new jbutton("most active");     findmostactivegeyser.addactionlistener(this);     add(findmostactivegeyser, position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 7;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);      findleastactivegeyser = new jbutton("least active");      findleastactivegeyser.addactionlistener(this);     add(findleastactivegeyser, position);      position = new gridbagconstraints();     position.gridx = 6;     position.gridy = 8;      position.gridwidth = 1;     position.gridheight = 1;     position.insets =  new insets(0,5,5,5);      getgeyserlist = new jbutton("geyser list");       getgeyserlist.addactionlistener(this);     add(getgeyserlist, position);      // set file menus     setupmenus();     pack();  }  /********************************************************************* list entries given arraylist of eruptions.  include final line number of eruptions listed  @param m list of eruptions  *********************************************************************/ private void displayeruptions(arraylist <eruption> m){      resultsarea.settext("");      for(eruption e: m){          resultsarea.append("\n" + e.tostring());      }     resultsarea.append ("\nnumber of geysers: " + m.size()); }  /********************************************************************* respond menu selections , button clicks  @param e button or menu item selected  *********************************************************************/ public void actionperformed(actionevent e){      eruption item = null;      // either open file or warn user     if (e.getsource() == openitem){         openfile();          }else if(db.getnumeruptions() == 0){         string errormessage = "did forget open file?";         resultsarea.settext(errormessage);         return;         }       // menu item - quit     else if (e.getsource() == quititem){         system.exit(1);     }      // fix me: count menu item - display number of eruptions , geysers        else if (e.getsource() == countitem){         resultsarea.settext("\nnumber of eruptions: " + db.getnumeruptions());     }      // fix me: display late night eruption     else if (e.getsource() == findlatenighteruption){        resultsarea.settext("latest eruption\n" + db.getlatenighteruption());       }      //fix me: display geyser names       else if (e.getsource() == getgeyserlist){          displayeruptions(db.geteruptionlist());     }      //fix me: max eruptions day in year (check year)      else if(e.getsource() == findmaxeruptionsinyear){         string aux = year.gettext();          if(aux.length() == 0){              joptionpane.showmessagedialog(this, "enter year search maax eruptions");          }         else         {             int y = integer.parseint(year.gettext());             string result = db.finddaywithmosteruptions(y);             resultsarea.settext(result);          }      }     // fix me: list eruptions geyser (check  name)     else if(e.getsource() == findgeyserbyname){          string name = geyser.gettext();         if(name.length() == 0){             joptionpane.showmessagedialog(this, "provide name");          }         else         {             arraylist<eruption>result = db.geteruptions(name);             displayeruptions(result);         }      }      // fix me: display eruptions particular date     else if(e.getsource()==findamountoferuptionsondate){           string aux1 = year.gettext();         string aux2 = month.gettext();         string aux3 = day.gettext();          if(aux1.length() == 0 || aux2.length() == 0 || aux3.length() == 0)         {             joptionpane.showmessagedialog(this, "provide year,month, , day");         }         else         {             int y = integer.parseint(year.gettext());             int m = integer.parseint(month.gettext());             int d = integer.parseint(day.gettext());              int result = db.getnumeruptions(m, d, y);             resultsarea.settext("number of eruptions: " + result);         }      }     else if(e.getsource() == findmostactivegeyser){         resultsarea.settext("most active geyser: " + db.findmostactivegeyser());      }      else if(e.getsource() == findleastactivegeyser){          resultsarea.settext("least active geyser: " + db.findleastactivegeyser());      }  }   /********************************************************************* in response menu selection - open data file  *********************************************************************/ private void openfile(){     jfilechooser fc = new jfilechooser(new file(system.getproperty("user.dir")));     int returnval = fc.showopendialog(this);      // did user select file?     if (returnval == jfilechooser.approve_option) {         string filename = fc.getselectedfile().getname();         // use name of lottery ticket variable           db.readgeyserdata(filename);               } }  /********************************************************************* create custom gridbag constraint  *********************************************************************/     private gridbagconstraints makeconstraints(int x, int y, int h, int w, int align){      gridbagconstraints rtn = new gridbagconstraints();      rtn.gridx = x;      rtn.gridy = y;      rtn.gridheight = h;      rtn.gridwidth = w;       // set alignment: line_start, center, line_end     rtn.anchor = align;      return rtn;  }     /********************************************************************* set menu items  *********************************************************************/ private void setupmenus(){      // create menu components     filemenu = new jmenu("file");     quititem = new jmenuitem("quit");     openitem = new jmenuitem("open...");     reportsmenu = new jmenu("reports");     countitem = new jmenuitem("counts");      // assign action listeners     quititem.addactionlistener(this);     openitem.addactionlistener(this);     countitem.addactionlistener(this);      // display menu components     filemenu.add(openitem);     filemenu.add(quititem);     reportsmenu.add(countitem);         menus = new jmenubar();      menus.add(filemenu);     menus.add(reportsmenu);     setjmenubar(menus);     }              } 

geyser code:

 import java.util.scanner; public class geyser { string geysername; int count = 0; public geyser (string name){    geysername = name;  }  public void increment (){        count++;   }  public string getname() {     return geysername; }  public int getnumeruptions() {     return count; }  public static void main(string args[]) {     geyser g = new geyser("xyz");     if (g.getname().equals("xyz")) {         system.out.println("getname worked well.");     }     else {         system.out.println("getname did not work well.");     }     if (g.getnumeruptions() == 0) {         system.out.println("getnumeruptions worked well.");     }     else {         system.out.println("getnumeruptions did not work well.");     }     g.increment();       if (g.getnumeruptions() == 1) {         system.out.println("getnumeruptions worked well.");     }     else {         system.out.println("getnumeruptions did not work well.");      }     }    } 

eruption code:

import java.util.scanner; public class eruption { int month; int day; int year; int hour; int minute; string geysername;  public eruption(string info){      scanner scnr = new scanner(info);     scnr.usedelimiter("/|,|:");     month = scnr.nextint();     day = scnr.nextint();     year = scnr.nextint();       geysername = scnr.next();     hour = scnr.nextint();      minute = scnr.nextint();    }  public int getmonth(){      return month;  }  public int getday(){      return day;  }  public int getyear(){      return year; }  public int gethour(){      return hour;  }  public int getminute(){      return minute; }  public string getgeysername(){      return geysername; }  public  string tostring( ){      string geyserstring = geysername + " " + "on" + month +"/"+ day +"/" + year + "at" + hour +":"+ minute;     return geyserstring;  }  public static void main(string [] args){     eruption e = new eruption("1/2/2016,old faithful,10:46");     if (e.getmonth() == 1) {         system.out.println("getmonth worked well");         }       }      } 


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -