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

I have a new error message: Main method not found in class Titanic, please defin

ID: 3758972 • Letter: I

Question

I have a new error message: Main method not found in class Titanic, please define the main method as: public static void main(Strings[] args). When I place it in the code where I think it should go (underneath public class Titanic{ ), I get 30 or so new errors. How do I correct this? Thanks!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.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=null;
       try {
          fs=new Scanner(new File(filename));
          }catch(FileNotFoundException e) {
               System.out.print("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();

       }

   }

   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;
   }

   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;
   }
}

Explanation / Answer

/* It is a issue of comiler and jRE compilance version like If you have compiled your code using JDK7 and Run using JRE6 then It will create Problems otherwise there is no error in Code. I have mentioned Main method in bold and attach output screen also with your same code. It is working fine.*/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.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=null;
       try {
          fs=new Scanner(new File(filename));
          }catch(FileNotFoundException e) {
               System.out.print("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();

       }

   }

   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;
   }

   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;
   }

   public static void main(String[] args)
   {
       System.out.println("XYZ");
     
   }

}