I am only showing the spaceport and fileIO. I want the first line of my txt file to show the number of spaceship to create an array then choose what type of spaceship. I did the best i can and now i am completly stuck its not working. Can Someone please help. The Instruction are:
The relationSpaceShips between the classes in this project is as follows:
(1) SpacePort has-many SpaceShip
(2) SpaceShip has-a Captain
(3) SpaceShip has-a Engine
(4) SpaceShip has-a RegistrationDate
(5) Impulse is-a Engine
(6) Warp is-a Engine

(7) SpaceShip has-many Crewman
(6) Person has-a pid (int)
(9) Person has-a DateOfBirth
(10) Captain is-a Officer
(11) Officer is-a Person

(12) DateOfBirth is-a StarDate
(13) RegistrationDate is-a StarDate
(14) StarDate has-a day (int)
(15) StarDate has-a month (int)
(16) StarDate has-a year (int)

(17) Enterprise is-a SpaceShip
(18) WarBird is-a SpaceShip
(19) RedShirt is-a Crewman
(20) Crewman is-a Person

NOTE:every object has:
private String objName, and
public String getName() {return objName;}


Create a test program that creates an SpacePort object, and prompts for
the user to enter the path of a data file. The program then reads the data from the data
file for the SpacePort.
Then, print:
1. all SpaceShip objects belonging to the SpacePort, sorted by getName(), and
2. all Person objects belonging to the SpacePort, sorted by getName()

Test your program by creating a data file and filling it in
with the data that answers the questions. Include the
data file and run.bat in the project folder.

package space_shipPKG;
import fileIOPKG.*;
import jdUtil.*;
public class SpacePort  {
    
    private SpaceShip[] ship;
    private int num_ships;
    
    public SpacePort(){
        
        readFromFile();
  
    }
        
              public void readFromFile(){
                  try {
                      String s = Utils.getSystemIn("Please enter the name of the file you would like. The file name is Assign02:");
                      FileIO fio=new FileIO(s);
                      while(fio.hasNextLine())
                      {
                           if(fio.hasNextInt()==false){
                                  throw new Exception("Dealer.readFromFile:missing numships");
                                }
                                 int num_ships = fio.getNextInt();
                                 ship = new SpaceShip[num_ships];
                                 for(int i=0;i<ship.length;i++){
                                  if(fio.getNextLine()==null){
                                    throw new Exception("Dealer.readFromFile:missing numships");
                                  }
                                  String cartype=fio.getNextLine();
                                  if(cartype.equals("Enterprise")){
                                    ship[i]= new Enterprise();
                                  }else{
                                    ship[i]= new WarBird();
                                  }
                                  ship[i].readFromFile();
                                }//for each car in carrArray
                                
                               }//readFromFile
                      
                  } catch (Exception e) {
                      System.out.println("\nin MainClass e=\n"+e.toString());
                      e.printStackTrace();
                  }
              }
              
          
          
     
    private String objName;
    public String getName()
     {
         return objName;
     }
        
     
     public String toString(){
         
         String test="??????";
         return test;
            
            }
    
          
}
package fileIOPKG;
     import java.util.*;
     import java.io.*;
     public class FileIO {
	//this class acts as a static "producer" class that,after being
	//initialized with a filename,
	private FileReader freader;
	private Scanner scan=null;
	public FileIO(){
		freader=null;
		scan=null;
	}//default constructor
	public FileIO(String filename) throws FileNotFoundException{

		File infile;

		FileReader reader;		
		infile = new File(filename);

		reader = new FileReader(infile);
		scan = new Scanner(reader);
	}//(String) constructor
	public boolean hasNextInt() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			return scan.hasNextInt();
		}
	}//hasNextInt
	public int getNextInt() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			int iv=scan.nextInt();
			System.out.println("FileIO.getNextInt:"+iv);
			return iv;
		}
	}//getNextInt
	
	public boolean hasNextLine() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			return scan.hasNextLine();
		}
	}//hasNextLine
	public String getNextLine() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			String result=scan.nextLine().trim();
			System.out.println("FileIO.getNextLine:"+result);
			return result;
		}
	}//getNextLine

	
	public boolean hasNextDouble() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			return scan.hasNextDouble();
		}
	}//hasNextDouble
	public double getNextDouble() throws Exception{
		if(scan==null){
			throw new Exception("FileIO.getNextInt:file not opened");
		}else{
			double dv=scan.nextDouble();
			System.out.println("FileIO.getNextDouble:"+dv);
			return dv;
		}
	}//getNextDouble
	public void close(){
		try {
			freader.close();
		} catch (IOException e) {
			//ignore
		}
	}//close

}//class FileIO

Recommended Answers

All 2 Replies

You posted a lot of code. If you post any amount of code, you should use code tags. Reading the forums rules would have told you this (as would general observation, i.e. looking at the reply box)

The way I see it, first you need to create the file and fill it with data. Then try to read it. The tricky part would be to figure out the format of the file. For example if you want to represent a crewman's data you can do this:

Crewman,personName,pid,year,month,day,

After you read this line use String.split(",") method and create a Crewman instance with the above data.
But since there would be many spaceships with many crewmen, you might want to try a more tree structure approach for your file:

The number 5 is the number of SpaceShips and the 10 the number of crewmen

5
<SpaceShip>
spaceShipName
<Crewman>
10
Crewman,personName,pid,year,month,day,
Crewman,personName,pid,year,month,day,
Crewman,personName,pid,year,month,day,
.......
</Crewman>
year,month,day, // registration date
engineName
Captain,personName,pid,year,month,day,
</SpaceShip>
.....
<SpaceShip>
....
</SpaceShip>

I tried to keep it simple and not confuse you with xml. The more appropriate way would be this:

<SpacePort>
<NumOfSpaceShips>5</NumOfSpaceShips>
<SpaceShip>
<SpaceShipName>uss177</SpaceShipName>
<SpaceShipType>Enterprise</SpaceShipType>
<Crewmen>
<NumOfCrewmen>10</NumOfCrewmen>
<Crewman>
<name>Data</name>
<pid>01010101</pid>
<year>0</year>
<month>0</month>
<day>0</day>
</Crewman>
<Crewman>
.....
</Crewman>
.....
</Crewmen>
<RegistrationDate>
<year>2555</year>
<month>12</month>
<day>26</day>
</RegistrationDate>
<EngineName>engine</EngineName>
<EngineType>warp</EngineType>
<captain>
<name>Picard</name>
<pid>112112</pid>
<year>2500</year>
<month>12</month>
<day>21</day>
</captain>
</SpaceShip>
.....


<SpacePort>

If you don't want to mess with xml parsers, you can try and write a program with lots of ifs statement were you read the file line by line and depending on what you read (<Crewman>, <captain>) you will know what will the next lines will have.
Example when you read the line <Crewman> you will know that the next lines:
<name>Data</name>
<pid>01010101</pid>
<year>0</year>
<month>0</month>
<day>0</day>
until you reach
</Crewman> have the data of a single crewman

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.