Question: Could someone please debug this program. It should create a driver class called FlotillaDriver that is required to Open the text file called flotilla.csv and

Could someone please debug this program. It should create a driver class called FlotillaDriver that is
required to
Open the text file called flotilla.csv and load all ship using
the appropriate subclass into an ArrayList dedicated to the
super class Ship.
Instantiate the proper sub ship object to add to the list.Only the basic ship information in the initial list,
but when creating the displays when one of the buttons is
clicked, you must provide the additional information of the
individual sub ship. After you have successfully loaded the data, you need to con
figure a popup using the showOptionsDia
log static method of the JOptionPane class.
The Option buttons on this pop-up should be for each sub
ship and one to quit the program. Clicking on one of the sub
ship options will produce the popups.
Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at FlotillaDriver.displayMenu(FlotillaDriver.java:79)
at FlotillaDriver.main(FlotillaDriver.java:24)
C:\Users\bigbe\AppData\Local\NetBeans\Cache\18\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\bigbe\AppData\Local\NetBeans\Cache\18\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 6 seconds)
Program:
public class FlotillaDriver {
private ArrayList shipList;
public FlotillaDriver(){
shipList = new ArrayList>();
}
public static void main(String[] args){
FlotillaDriver driver = new FlotillaDriver();
driver.loadShips("flotilla.csv");
driver.displayMenu();
}
private void loadShips(String filename){
try (BufferedReader br = new BufferedReader(new FileReader(filename))){
String line;
while ((line = br.readLine())!= null){
String[] shipData = line.split(",");
// Assume the first element indicates the ship type
Ship ship;
switch (shipData[0]){
case "CG":
ship = new CargoShip(
shipData[1], shipData[2], Integer.parseInt(shipData[3]),
Integer.parseInt(shipData[4]), Integer.parseInt(shipData[5]),
Integer.parseInt(shipData[6]), Integer.parseInt(shipData[7]),
Integer.parseInt(shipData[8]));
break;
case "YT":
ship = new Yacht(
shipData[1], shipData[2], Integer.parseInt(shipData[3]),
Integer.parseInt(shipData[4]), Integer.parseInt(shipData[5]),
Integer.parseInt(shipData[6]), Integer.parseInt(shipData[7]),
Integer.parseInt(shipData[8]), Integer.parseInt(shipData[9]),
Integer.parseInt(shipData[10]),Integer.parseInt(shipData[11]));
break;
case "CR":
ship = new CruiseShip(
shipData[1], shipData[2], Integer.parseInt(shipData[3]),
Integer.parseInt(shipData[4]), Integer.parseInt(shipData[5]),
Integer.parseInt(shipData[6]), Integer.parseInt(shipData[7]),
Integer.parseInt(shipData[8]), Integer.parseInt(shipData[9]));
break;
default:
// Default to Ship if type is not recognized
ship = new Ship(
shipData[1], shipData[2], Integer.parseInt(shipData[3]),
Integer.parseInt(shipData[4]), Integer.parseInt(shipData[5]),
Integer.parseInt(shipData[6]));
}
shipList.add(ship);
}
} catch (IOException e){
JOptionPane.showMessageDialog(null, "Error reading file: "+ e.getMessage(),
"File Read Error", JOptionPane.ERROR_MESSAGE);
}
}
private void displayMenu(){
String[] options = shipList.stream().map(Ship::getName).toArray(String[]::new);
String menuMessage = "Choose a ship to view details or press 'Cancel' to exit.";
int response;
do {
response = JOptionPane.showOptionDialog(null, menuMessage, "Ship Selector",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (response >=0 && response shipList.size()){
Ship selectedShip = shipList.get(response);
JOptionPane.showMessageDialog(null, selectedShip.toString(),
"Ship Details", JOptionPane.INFORMATION_MESSAGE);
}
} while (response != JOptionPane.CLOSED_OPTION);
}
}
 Could someone please debug this program. It should create a driver

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!