读取txt文件 [英] reading txt file

查看:78
本文介绍了读取txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅显示空间端口和fileIO.我希望我的txt文件的第一行显示要创建一个数组的太空飞船的数量,然后选择哪种类型的太空飞船.我已尽力而为,现在我完全陷入了困境.有人可以帮忙吗?指令是:
该项目中的类之间的RelationSpaceShips如下:
(1)SpacePort有许多SpaceShip
(2)太空船有一名机长
(3)太空船有引擎
(4)SpaceShip具有一个RegistrationDate
(5)脉冲is-a引擎
(6)扭曲是引擎

(7)太空船有很多船员
(6)人有一个pid(int)
(9)人有-出生日期
(10)上尉是军官
(11)官员是一个人

(12)DateOfBirth是-StarDate
(13)RegistrationDate是-StarDate
(14)StarDate有一天(int)
(15)StarDate有一个月(int)
(16)StarDate已有一年(int)

(17)Enterprise is-a SpaceShip
(18)WarBird is-a SpaceShip
(19)RedShirt是一名船员
(20)船员是一个人

注意:每个对象都有:
私有字符串objName和
public String getName(){返回objName;}


创建一个测试程序,该程序创建一个SpacePort对象,并提示输入
用户输入数据文件的路径.然后程序从数据中读取数据
SpacePort的文件.
然后,打印:
1.属于SpacePort的所有SpaceShip对象,按getName()和
排序 2.属于SpacePort的所有Person对象,按getName()排序
通过创建数据文件并将其填充来测试您的程序
与回答问题的数据.包括
数据文件和run.bat放在项目文件夹中.

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

推荐答案

polska03写道:
polska03 wrote:

我尽了我所能,现在我完全卡住了,无法正常工作.

I did the best i can and now i am completly stuck its not working.



好的,但是您真的需要通过解释什么不起作用来帮助我们.当您说不工作"时,可能意味着从打印消息"到使我的房子着火"之类的任何内容.尝试指出错误在代码中的何处发生,实际的错误是什么以及您观察到的其他任何内容;记住要使用调试器来协助.并且如果确实发布了任何编译器,异常或系统消息,请确保使用复制和粘贴操作,以便我们可以看到消息的确切措辞.



OK, but you really need to help us by explaining what is not working. When you say ''not working'' that could mean anything from "prints a message" to "sets my house on fire". Try to indicate where in your code the error occurs, what the actual error is, and anything else that you observe; remember to use the debugger to assist. And if you do post any compiler, exception or system messages, ensure you use copy and paste so we can see the exact wording of the message.


这篇关于读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆