简体   繁体   English

需要帮助,尝试读取文件并使用Java中的参数填充对象数组

[英]Need help, trying to read a file and fill an array of objects with their parameters in java

To let you know this is an assignment for my class, I'm just asking for a little help with it. 为了让您知道这是我班的作业,我只是在寻求一点帮助。 The short form of the assignment is to: 分配的简称是:

Write an application class that reads a file (from the command line) and fills an array of type vehicle[] with new vehicle (params), new car (params), new american car (params) new foreign car(params) , new truck (params), new bicycle (params), etc. 编写一个应用程序类,该类读取文件(从命令行),并用新车(参数),新车(参数),新美国车(参数),新外国车(参数),新车填充数组vehicle []卡车(参数),新自行车(参数)等

This is what I have so far(I have already written individual classes for each of the objects that all extend vehicle): 到目前为止,这就是我所要做的(我已经为所有扩展车辆的对象编写了单独的类):

  public class n01029506{
  public static void main(String[] args) throws FileNotFoundException{   
     Scanner input = new Scanner(new File(args[0]));
     ArrayList<Vehicle> list = new ArrayList<>();

  while(input.hasNext()){
     if(input.nextLine().equals("vehicle")){
        String name = input.nextLine();
        String address = input.nextLine();
        String phone = input.nextLine();
        String email = input.nextLine();       
        list.add(new Vehicle(name, address, phone, email));
     }
    }
    System.out.println(list.get(0));
   }
  }

The output of this code is an array of objects called vehicle from the file I am reading. 该代码的输出是我正在读取的文件中的一个名为vehicle的对象数组。 The problem I am having is when I add another else if statement it compiles but I get a runtime error. 我遇到的问题是当我添加另一个if语句可以编译但我遇到运行时错误时。

     if(input.nextLine().equals("vehicle")){
        String name = input.nextLine();
        String address = input.nextLine();
        String phone = input.nextLine();
        String email = input.nextLine();       
        list.add(new Vehicle(name, address, phone, email));
     }else if(input.nextLine().equals("car")){
        String name = input.nextLine();
        String address = input.nextLine();
        String phone = input.nextLine();
        String email = input.nextLine();    
        boolean convert = input.nextBoolean();
        String color = input.nextLine();   
        list.add(new Car(name, address, phone, email, convert, color));
     }

What I need the program to do is go through and once it reads a line from the file - whether a car, vehicle, etc - it creates and object and stores it in the array. 我需要程序执行的工作是,一旦它从文件中读取一行(无论是汽车,车辆等),它就会创建对象并存储在数组中。 I'm not sure how to get the program to work properly. 我不确定如何使程序正常运行。 If anyone could help me in the right direction I would really appreciate it. 如果有人能在正确的方向上帮助我,我将非常感激。

I will try to spell out what JB Nizet said in the comment. 我将尝试阐明JB Nizet在评论中所说的内容。 Assume your input file contains: 假设您的输入文件包含:

car
Riley Clark
3476 9th Ave N
555-555-1234
rc@toyota.jp
false
blue

OK? 好? Now your program reads the line car and compares it to vehicle . 现在你的程序读取线car ,并将其与vehicle Since they are not the same, it goes into the else if part. 由于它们不相同,因此将其包含在else if部分中。 This will read the line Riley Clark and compare it to car . 这将读取行Riley Clark并将其与car进行比较。 They are not the same either. 他们也不一样。 So you program will never discover there's a car in these lines of the file. 因此,您的程序将永远不会在文件的这些行中发现有汽车。

JB Nizet also gave the solution: “Call nextLine() once , store the result in a variable, and compare this variable with "vehicle", "car", etc.” JB Nizet还提供了解决方案:“ 一次调用nextLine() ,将结果存储在变量中,然后将此变量与“车辆”,“汽车”等进行比较。”

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

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