简体   繁体   English

从txt文件将多种数据类型读取到arrayList-Java

[英]Read multiple data types from txt file into arrayList - Java

For this program I'm supposed to read from a txt file containing information on the automobile's make , model , mpg , and trunk space in that order. 对于该程序,我应该从一个txt文件中读取该文件,该文件包含有关汽车的makemodelmpgtrunk space An example is: hyundai genesis 24.6 100 and repeated for several different cars. 一个例子是:现代创世24.6 100,并针对几种不同的汽车重复进行。 We had to construct an " Automobile " superclass with the instance variables of make and model. 我们必须使用make和model的实例变量构造一个“ Automobile ”超类。 Then a " GasEngineAuto " subclass with instance variable for mpg that extends " Automobile ". 然后是“ GasEngineAuto ”子类, GasEngineAuto具有mpg的实例变量,扩展了“ Automobile ”。 Then a subclass called " Sedan " that extends " GasEngine Auto " and has instance variable for trunk space. 然后是一个名为“ Sedan ”的子类,该子类扩展了“ GasEngine Auto ”并具有用于后备箱空间的实例变量。 For the assignment we had to get these classes signed off on to make sure that they made correctly. 对于作业,我们必须将这些类签名,以确保它们正确制作。

Write a method to read the information from the file gas_sedans.txt and store it in the list you created in the previous step. 编写一种方法来从文件gas_sedans.txt读取信息,并将其存储在上一步中创建的列表中。 The list should be the only parameter for this method. 该列表应该是此方法的唯一参数。 Within the method, open the file, read the information for each sedan into appropriately-typed variables, call the parameterized constructor (the one that takes arguments for all attributes) to create the object, then add the object to the list. 在该方法内,打开文件,将每个轿车的信息读入适当类型的变量中,调用参数化的构造函数(该函数接受所有属性的参数)以创建对象,然后将该对象添加到列表中。 Call this method from main. 从main调用此方法。

Below is my code so far. 下面是到目前为止的代码。 I wanted to try to make sure I could read into the arrayList before I used a method to do it. 我想尝试确保在使用某种方法之前可以读入arrayList。

 import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author */ public class lab10_prelab { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { ArrayList<Sedan> sedanList = new ArrayList<Sedan>(); File myFile = new File(System.getProperty("user.dir")+"/src/gas_sedans (1).txt"); if (!myFile.exists()){ System.out.println("The file could not be found"); System.exit(0); } Scanner inputFile = new Scanner(myFile); while (inputFile.hasNext()) { Sedan a = new Sedan(); a.setMake(inputFile.nextLine()); a.setModel(inputFile.nextLine()); inputFile.nextLine(); a.setMPG(inputFile.nextDouble()); inputFile.nextLine(); a.setTrunkCapacity(inputFile.nextDouble()); sedanList.add(a); } inputFile.close(); } } 

It says that I get a InputMismatchException notice when I try to run the program. 它说我尝试运行该程序时收到InputMismatchException通知。

You have typo error: automobileArray and autombileArray are different. 你有错字错误: automobileArrayautombileArray是不同的。

In your code you are missing o after m in the variable 在您的代码中,变量中的m后缺少o

autombileArray
    ^

Edited: 06:49am 18/07/20115 编辑时间:2011年7月18日06:49

It says that I get a InputMismatchException notice when I try to run the program. 它说我尝试运行该程序时收到InputMismatchException通知。

a.setModel(inputFile.nextLine());
inputFile.nextLine();//remove this line
a.setMPG(inputFile.nextDouble());
inputFile.nextLine();
a.setTrunkCapacity(inputFile.nextDouble());
inputFile.nextLine();//add here

I made some changes and arrived at this. 我进行了一些更改并达到了此目的。 Now it says to: Write a method to display the list. 现在它说:写一个显示列表的方法。 The method should have one parameter, an ArrayList of Automobiles. 该方法应具有一个参数,即“汽车的ArrayList”。 Within the body of the method, call upon the toString method to display each item. 在方法的主体内,调用toString方法以显示每个项目。 Call this method at the end of main. 在main的末尾调用此方法。 Does this mean make a class like "public String toString()... or is what I did fine. 这是否意味着要制作一个类似于“ public String toString()...”的类,还是我做的很好。

 public class Smith_lab10_prelab { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { ArrayList<Automobile> AutomobileList = new ArrayList<Automobile>(); fillAutomobileList(AutomobileList); displayAutomobileList(AutomobileList); } public static void fillAutomobileList(ArrayList sedanList) throws FileNotFoundException { File myFile = new File(System.getProperty("user.dir") + "/src/gas_sedans (1).txt"); if (!myFile.exists()) { System.out.println("The file could not be found"); System.exit(0); } Scanner inputFile = new Scanner(myFile); for (int i = 0; inputFile.hasNext(); i++) { Sedan a = new Sedan(); a.setMake(inputFile.next()); a.setModel(inputFile.next()); a.setMPG(inputFile.nextDouble()); a.setTrunkCapacity(inputFile.nextDouble()); sedanList.add(a); } } public static void displayAutomobileList(ArrayList automobileList) { System.out.println(automobileList.toString()); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM