我正在尝试完成一个使用图形用户界面搜索美国间歇泉数据库的项目。我的一个数组列表一直出错。我会包含我的程序数据,但是长达10,000个条目。任何帮助非常感谢。这是在BlueJ
中完成的错误:java.lang.IndexOutOfBoundsException:索引:0,大小:0
数据库代码:
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 as long as there is 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 to read the 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;
for ( 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();
for ( 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 > ();
for ( 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 a list of geysers
ArrayList<Geyser>geyserList = new ArrayList<Geyser>();
for(String s:nameList){
Geyser g = new Geyser(s);
// count number of eruptions for 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代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.text.*;
/***********************************************************************
* GUI front end for a 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 and displays all of the GUI commponents
*********************************************************************/
public GeyserGUI(){
db = new GeyserDatabase();
// FIX ME: the following line should be removed
db.readGeyserData("GeyserData.txt");
// create the Gridbag layout
setLayout(new GridBagLayout());
GridBagConstraints position = new GridBagConstraints();
// create the 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 up File menus
setupMenus();
pack();
}
/*********************************************************************
List all entries given an ArrayList of eruptions. Include a final
line with the 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 to menu selections and button clicks
@param e the button or menu item that was selected
*********************************************************************/
public void actionPerformed(ActionEvent e){
Eruption item = null;
// either open a file or warn the user
if (e.getSource() == openItem){
openFile();
}else if(db.getNumEruptions() == 0){
String errorMessage = "Did you forget to open a 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 and 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 all geyser names
else if (e.getSource() == getGeyserList){
displayEruptions(db.getEruptionList());
}
//FIX ME: max eruptions day in a year (check for year)
else if(e.getSource() == findMaxEruptionsInYear){
String aux = Year.getText();
if(aux.length() == 0){
JOptionPane.showMessageDialog(this, "Enter a Year to Search For Maax Eruptions");
}
else
{
int y = Integer.parseInt(Year.getText());
String result = db.findDayWithMostEruptions(y);
resultsArea.setText(result);
}
}
// FIX ME: list all eruptions for a geyser (check for 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 for a 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, and 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 to the menu selection - open a data file
*********************************************************************/
private void openFile(){
JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir")));
int returnVal = fc.showOpenDialog(this);
// did the user select a file?
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filename = fc.getSelectedFile().getName();
// use the name of your lottery ticket variable
db.readGeyserData(filename);
}
}
/*********************************************************************
Create a 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 up the 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);
}
}
间歇泉代码:
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.");
}
}
}
火山喷发码:
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");
}
}
}