Need Help with my project

0 Solid Krieger · October 8, 2015
Trying to make this code work to these specifications.  

 1. (25 points) Design a Java application that will read a file containing data related to the passengers on the Titanic. The description of the file is shown below. The application should provide statistical results on the passengers including: 
a. Total number of passengers on the Titanic 
b. Total number of passengers who perished on the Titanic 
c. Total number of Passengers who survived the sinking of the Titanic 
d. Number of passengers who survived the sinking of the Titanic as a function of the passenger class (e.g. 1,2,3) 
e. Number of passengers who survived the sinking of the Titanic as a function of the passenger gender (e.g., male, female) 
f. A list of the names of passengers who paid greater than $200 for their tickets 
g. A list of the names of passengers who were less than 10 years old who survived the sinking of the Titanic 
h. A list of the names of passengers who were less than 10 years old who perished on the Titanic 
i. The count of the number of passengers as a function of the first letter of their last name. (e.g., A: 13, B:33 …) 
j. Additional statistical results you add to enhance the functionality 

The following are some design criteria and specific requirements that need to be addressed: 
a. Use command line arguments to send in the name of the Titanic file. 
b. Use a 2D array to store the Titanic data. (Hint: You will probably need to store the array as String values and then convert to other types as needed since some data is null) 
c. You should create at least 2 Java classes – Titanic and TestTitanic. You are welcome to create additional classes if you want to further separate the functionality. 
d. You should create separate methods for each of the required functionality. (e.g. getTotalPassengers() will return the total number of passengers on the Titanic.) 
e. A user-friendly and well-organized menu should be used for users to select which data to return. A sample menu is shown in run example. You are free to enhance your design and you should add additional menu items and functionality. 
f. The menu system should be displayed at the command prompt, and continue to redisplay after results are returned or until Q is selected. If a user enters an invalid menu item, the system should redisplay the menu with a prompt asking them to enter a valid menu selection 
g. The application should keep track of the elapsed time (in seconds) between once the application starts and when the user quits the program. After the program is exited, the 

application should provide a prompt thanking the user for trying the Titanic program and providing the total time elapsed. 

I keep getting error messages and it is not pulling the file. What am I doing wrong? Please help

package titanic;

import java.util.Scanner;

public class Titanic {
    private static final int X = 1309;
    private static final int Y = 6;
    public String[][] PassengerData(String fileName) throws IOException {
        // Declare Scanner s
        Scanner s = null;
        // Array Declaration/Initialization
        try {         
            s = new Scanner(new BufferedReader(new FileReader(fileName)));                                       
        // Declare/Initialize Array
            String[][] passengerData = new String[X][Y];
            for(int i = 0; i < passengerData.length; i++) {
                String line = s.nextLine();
                String[] tokens = line.split("[\t,]");
                for(int j = 0; j < passengerData.length; j++) {                   
                    passengerData[j] =;
                } // end for
            } // end for
            return passengerData;
        } finally {
            System.out.println("Exception found in PassengerData.");
        } // end IOException  
    } // end PassengerData method
} // end Titanic class


package titanic;

 * File:
 * Purpose: Implement Titanic Class using Command Line Arguments/UserInput.

import java.util.Scanner;
public class TestTitanic {
    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();
        long stopTime = 0;
        String fileName = '"' + args[0] + '"';
        // Instantiate Titanic object
        Titanic titanic = new Titanic();
        PrintWriter pw = null;
            File f = new File("Users/Solidkrieger/Documents/UMUC Fall/INTRODUCTION TO PROGRAMMING/Week 8/Final Project/Titanic Statistic.text");
            pw = new PrintWriter(f);
            pw.println("Titanic Statistic.text");
        } catch(Exception e) {
        } finally {
        // Inititalize array with data from text file.
        } catch(Exception e) {
        // Welcome Message
        System.out.println("*******Welcome to the Titanic Statistical "
            + "Application*******\n");
        // Call in Scanner
        Scanner s = new Scanner(;
        // User Input Selection Branch
        String choice = "";
        while(choice != "Q") {
            System.out.println("Enter the number of the question you want "
                    + "answered. Enter ‘Q’ to quit the program: \n");
            System.out.println("1. How many passengers were on the Titanic?");
            System.out.println("2. What percentage of passengers perished on "
                    + "the Titanic?");
            System.out.println("3. What percentage passengers survived the "
                    + "sinking of the Titanic?");
            System.out.println("4. What percentage of passengers survived for "
                    + "each of the three classes?");
            System.out.println("5. What percentage of passengers survived as a "
                    + "function of gender?");
            System.out.println("6. What specific passengers paid more than $200 "
                    + "for their tickets?");
            System.out.println("7. What specific passengers who were less than "
                    + "10 years old perished on the titanic?");
            System.out.println("8. What specific passengers who were less than "
                    + "10 years old survived the sinking of the titanic?");
            System.out.println("9. For each letter in the alphabet, how many "
                    + "passengers last names started with that letter?");
            System.out.println("Q. Quit the program");
            System.out.print("Enter your selection: ");
            choice =;
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                case "Q":
                    System.out.println("Thank you for trying the Titanic"
                            + " Program");
                    stopTime = System.currentTimeMillis();
                    System.out.println("Elapsed time in seconds was: " +
                            ((stopTime - startTime) / 1000));
                    System.out.println("Please enter a proper value.");
        } // end while
    } // end main
} // end TestTitanic class

Post a Reply


Oldest  Newest  Rating
0 Jon Z. · October 8, 2015
what's the error you get?
0 Solid Krieger · October 10, 2015
Instead of show anything it spits out this message.

I'm still new to java so I'm kind of stuck on what I'm doing wrong here.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at titanic.TestTitanic.main(
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
0 Jon Z. · October 11, 2015
That usually means that you are trying to access outside the boundary of the array.
When you access a 2D array you need to specify the 2 indexes (arr[x][y]).
0 Kuroodo Ditory · October 11, 2015
To add to what Jon said, if you have an array with 5 elements ( a length of 5), you can get each element by doing myArray[x] , where x is a value from 0-4 due to there being 5 elements. But if x turns out to be a value of 5 or more, then it is passed the length/bounds of the array.

It's like having 10 cabinets and someone tells you to open the 12th cabinet (there is no 12th cabinet).
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator