Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I keep getting one error when I run this code. The error is in the TestTitanic.j

ID: 3758919 • Letter: I

Question

I keep getting one error when I run this code. The error is in the TestTitanic.java and it says class, interface or enum expected. How do I correct this error? Thanks!


import java.until.Scanner;
// Titanic Class

public class Titanic{
   private int totalNoOfPsngr;
   private int percentPerished;
   private int percentSurvived;
   private int[] percentSurvivedByClass;
   private int[] percentSurvivedByGender;
   private int paidMoreThan200;
   private int lessThan10YrPerished;
   private int lessThan10YrSurvived;
   private String[] passengerNames;

   public Titanic(){
       totalNoOfPsngr=0;
       percentPerished=0;
       percentSurvived=0;
       percentSurvivedByClass=new int[3];
       percentSurvivedByGender=new int[2];
       paidMoreThan200=0;
       lessThan10YrPerished=0;
       lessThan10YrSurvived=0;
       passengerNames=new String[1310];

   }

   public void readFile(String filename){
       Scanner fs=new Scanner(new File(filename));
       totalNoOfPsngr=fs.nextInt();
       percentPerished=fs.nextInt();
       percentSurvived=fs.nextInt();
       percentSurvivedByClass[0]=fs.nextInt();
       percentSurvivedByClass[2]=fs.nextInt();
       percentSurvivedByClass[3]=fs.nextInt();
       percentSurvivedByGender[0]=fs.nextInt();
       percentSurvivedByGender[1]=fs.nextInt();
       paidMoreThan200=fs.nextInt();
       lessThan10YrPerished=fs.nextInt();
       lessThan10YrSurvived=fs.nextInt();
       for(int i=0;i<1310;i++){
           passengerNames[i]=f.next();

       }

   }

   public void displayMenu(){
       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");
   }

   public int getTtotalNoOfPsngr(){
       return totalNoOfPsngr;
   }

   public int getPercentPerished(){
       return percentPerished;
   }

   public int getPercentSurvived() {
       return percentSurvived;
   }

   public int[] getPercentSurvivedByClass() {
       return percentSurvivedByClass;
   }

   public int[] getPercentSurvivedByGender() {
       return percentSurvivedByGender;
   }

   public int getPaidMoreThan200() {
       return paidMoreThan200;

   }

   public int getLessThan10YrPerished() {
       return lessThan10YrPerished;
   }

   public int getLessThan10YrSurvived() {
       return lessThan10YrServived;
   }

   public String[] getPsngrNameStartingWith(char c) {
       String[] names=new String[1310];
       for(int i=0,k=0;i<1310;i++){
           if(passengerNames[i].charAt(0)==c) {
               names[k++]=passengerNames[i];
           }

       }

       return names;
   }
}


// TestTitanic Class
TestTitanic.java

public class TestTitanic {

   public static void main(String[] args) {

       Titanic t=new Titanic();
       Scanner s=new Scanner(System.in);
       t.readFile(args[0]);
       String ch;
       do{
           t.displayMenu();
           System.out.println("Enter your selection: ");
           ch=s.next();
           switch(ch){
               case "1":
                   t.getTtotalNoOfPsngr();
                   break;

               case "2":
                   t.getPercentPerished();
                   break;

               case "3":
                   t.getPercentSurvived();
                   break;

               case "4":
                   int[] arr=t.getPercentSurvivedByClass();
                   System.out.println("Class 1: "+arr[0]);
                   System.out.println("Class 2: "+arr[1]);
                   System.out.println("Class 3: "+arr[2]);
                   break;

               case "5":
                   int[] arr=t.getPercentSurvivedByGender();
                   System.out.println("Male: "+arr[0]);
                   System.out.println("Female: "+arr[1]);
                   break;

               case "6":
                   t.getPaidMoreThan200();
                   break;

               case "7":
                   t.getLessThan10YrPerished();
                   break;

               case "8":
                   t.getLessThan10YrSurvived();
                   break;

               case "9":
                   String[] names=t.getPsngrNameStartingWith();
                   for(int i=0;names[i]!=null;i++) {
                       System.out.println(names[i]);
                   }

               break;
           
           }
   
       }while(!ch.equals("q")&&!ch.equals("Q"));

   }
}

Explanation / Answer

// There were some errors which I fixed and commented what those were. If you still have problems, please provide me a sample data file so I can test it!

// Titanic.java

import java.io.File; // have to import io.File
import java.io.FileNotFoundException;
import java.util.Scanner; // It is util, not until
//Titanic Class
public class Titanic{
private int totalNoOfPsngr;
private int percentPerished;
private int percentSurvived;
private int[] percentSurvivedByClass;
private int[] percentSurvivedByGender;
private int paidMoreThan200;
private int lessThan10YrPerished;
private int lessThan10YrSurvived;
private String[] passengerNames;
public Titanic(){
totalNoOfPsngr=0;
percentPerished=0;
percentSurvived=0;
percentSurvivedByClass=new int[3];
percentSurvivedByGender=new int[2];
paidMoreThan200=0;
lessThan10YrPerished=0;
lessThan10YrSurvived=0;
passengerNames=new String[1310];
}
public void readFile(String filename){
Scanner fs = null; // definition should be inside try/catch
try {
       fs=new Scanner(new File(filename));
   } catch (FileNotFoundException e) {
       System.out.println("File not found");
   }
totalNoOfPsngr=fs.nextInt();
percentPerished=fs.nextInt();
percentSurvived=fs.nextInt();
percentSurvivedByClass[0]=fs.nextInt();
percentSurvivedByClass[2]=fs.nextInt();
percentSurvivedByClass[3]=fs.nextInt();
percentSurvivedByGender[0]=fs.nextInt();
percentSurvivedByGender[1]=fs.nextInt();
paidMoreThan200=fs.nextInt();
lessThan10YrPerished=fs.nextInt();
lessThan10YrSurvived=fs.nextInt();
for(int i=0;i<1310;i++){
passengerNames[i]=fs.next(); // The Scanner variable is fs, not f
}
}
public void displayMenu(){
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");
}
public int getTtotalNoOfPsngr(){
return totalNoOfPsngr;
}
public int getPercentPerished(){
return percentPerished;
}
public int getPercentSurvived() {
return percentSurvived;
}
public int[] getPercentSurvivedByClass() {
return percentSurvivedByClass;
}
public int[] getPercentSurvivedByGender() {
return percentSurvivedByGender;
}
public int getPaidMoreThan200() {
return paidMoreThan200;
}
public int getLessThan10YrPerished() {
return lessThan10YrPerished;
}
public int getLessThan10YrSurvived() {
return lessThan10YrSurvived; // spelling mistake
}
public String[] getPsngrNameStartingWith(char c) {
String[] names=new String[1310];
for(int i=0,k=0;i<1310;i++){
if(passengerNames[i].charAt(0)==c) {
names[k++]=passengerNames[i];
}
}
return names;
}
}

// TestTitanic.java

import java.util.Scanner;

//TestTitanic Class
TestTitanic.java
public class TestTitanic {
public static void main(String[] args) {
Titanic t=new Titanic();
Scanner s=new Scanner(System.in);
t.readFile(args[0]);
String ch;
do{
t.displayMenu();
System.out.println("Enter your selection: ");
ch=s.next();
switch(ch){
case "1":
t.getTtotalNoOfPsngr();
break;
case "2":
t.getPercentPerished();
break;
case "3":
t.getPercentSurvived();
break;
case "4":
int[] arr=t.getPercentSurvivedByClass();
System.out.println("Class 1: "+arr[0]);
System.out.println("Class 2: "+arr[1]);
System.out.println("Class 3: "+arr[2]);
break;
case "5":
int[] arr=t.getPercentSurvivedByGender();
System.out.println("Male: "+arr[0]);
System.out.println("Female: "+arr[1]);
break;
case "6":
t.getPaidMoreThan200();
break;
case "7":
t.getLessThan10YrPerished();
break;
case "8":
t.getLessThan10YrSurvived();
break;
case "9":
   System.out.print("Enter a character: ");
   char c = s.next().charAt(0);
String[] names=t.getPsngrNameStartingWith(c); // a parameter should be passed
for(int i=0;names[i]!=null;i++) {
System.out.println(names[i]);
}
break;
  
}
  
}while(!ch.equals("q")&&!ch.equals("Q"));
}
}