当前位置: 首页 > 知识库问答 >
问题:

从文本文件中逐行读取车辆,并将这些车辆传递到Java构造函数中

杨星纬
2023-03-14
vehicle
owner's name (string)
address (string)
phone (string)
email (string)

car - extends of vehicle 
owner's name (string)
address (string)
phone (string)
email (string)
true or false for convertible (boolean)
color (string)

american car - extends of car
owner's name (string)
address (string)
phone (string)
email (string)
true or false for convertible (boolean)
color (string)
true or false for made in Detroit (boolean)
true or false for union shop (boolean)

foreign car - extends of car
owner's name (string)
address (string)
phone (string)
email  (string)
true or false for convertible (boolean)
color (string)
country of manufacturer (string)
import duty (float)

bicycle - extends of vehicle 
owner's name (string)
address (string)
phone (string)
email (string)
number of speeds (int)

truck - extends of vehicle 
owner's name (string)
address (string)
phone (string)
email (string) 
number of tons (float)
cost of truck (float)
date purchased (format below in example)
foreign car
aMarioy
Mario's house
(777) 777-7777
gmario@mario.com
false
black
Italy
4415.91

truck
aDougy
Doug's house
(123) 456-7890
hdoug@doug.com
30
61234.56
8/10/2003

vehicle
aRobby
Rob's house
(987) 654-3210
irob@rob.com

bicycle
bTommy
Tom's house
(246) 810-1214
jtom@tom.com
7

truck
bGeorge
George's house
(666) 666-6666
kgeorge@george.com
25
51234.56
12/4/2004

vehicle
bTim
Tim's house
(111) 111-1111
tim@tim.com

bicycle
bJim
Jim's house
(555) 555-5555
Ajim@jim.com
5

american car
bJohn
John's house
(888) 888-8888
Bjohn@john.com
true
green
false
true

car
cKen
Ken's house
(999) 999-9999
Cken@ken.com
false
orange

foreign car
cMario
Mario's house
(777) 777-7777
Dmario@mario.com
false
black
Italy
4415.91

truck
zDoug
Doug's house
(123) 456-7890
Edoug@doug.com
30
61234.56
8/10/2003

vehicle
eRob
Rob's house
(987) 654-3210
Frob@rob.com

bicycle
fTom
Tom's house
(246) 810-1214
Gtom@tom.com
7

american car
gSam
Sam's house
(333) 333-3333
Hsam@sam.com
false
blue
true
false
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

public class test4{
   public static void main(String[] args)throws FileNotFoundException {
      try{
         Scanner file = new Scanner(new File(args[0]));
         //ArrayList<Vehicle> vehicleList = new ArrayList<>();
         String line = new String();
         String newLine = new String();
         ArrayList<String> s1List = new ArrayList<>();
         ArrayList<String> s2List = new ArrayList<>();
         ArrayList<String> s3List = new ArrayList<>();
         ArrayList<String> s4List = new ArrayList<>();
         ArrayList<String> s5List = new ArrayList<>();
         ArrayList<String> s6List = new ArrayList<>();

         while(file.hasNextLine()){
            line = file.nextLine();
            if(line.equals("vehicle")){
              while(file.hasNextLine()){
               newLine = file.nextLine();
               if(!(newLine.equals(""))){
               s1List.add(newLine);
               }
               if(newLine.equals("")){
                  break;
               }
              }
            }
            if(line.equals("truck")){
              while(file.hasNextLine()){
               newLine = file.nextLine();
               s2List.add(newLine);
               if(newLine.equals("")){
                  break;
               }
              }
            }
            if(line.equals("bicycle")){
              while(file.hasNextLine()){
               newLine = file.nextLine();
               s3List.add(newLine);
               if(newLine.equals("")){
                  break;
               }
              }
            }
            if(line.equals("car")){
              while(file.hasNextLine()){
               newLine = file.nextLine();
               s4List.add(newLine);
               if(newLine.equals("")){
                  break;
               }
              }
            }
            if(line.equals("american car")){
              while(file.hasNextLine()){
               newLine = file.nextLine();
               s5List.add(newLine);
               if(newLine.equals("")){
                  break;
               }
              }
            }
            if(line.equals("foreign car")){
              while(true){
               newLine = file.nextLine();
               s6List.add(newLine);
               if(newLine.equals("")){
                  break;
               }
              }
            }                                                            
         }
         /*
         String[] s1 = s1List.toArray(new String[0]);
         String[] s2 = s2List.toArray(new String[0]);
         String[] s3 = s3List.toArray(new String[0]);
         String[] s4 = s4List.toArray(new String[0]);
         String[] s5 = s5List.toArray(new String[0]);
         String[] s6 = s6List.toArray(new String[0]);
         */

         /*
         System.out.println(s1.length);


         for(String a: s1){
            System.out.println(a);
         }
         */


         /*
         for(String a: s1List){
            System.out.println(a);
         }
         for(String a: s2List){
            System.out.println(a);
         }
         for(String a: s3List){
            System.out.println(a);
         }
         for(String a: s4List){
            System.out.println(a);
         }
         for(String a: s5List){
            System.out.println(a);
         }
         for(String a: s6List){
            System.out.println(a);
         }
         */



      }catch(FileNotFoundException ex){
         System.out.println("File not found.");
      }
   }
   public boolean equals(Object o){
      if(this == o){
         return true;  
      }
      if(o == null){
         return false;
      }
      return false;
   }   
}


class Vehicle{
   private String owner = new String();
   private String address = new String();
   private String phone = new String();
   private String email = new String();

   public Vehicle(String[] strArr){
      owner = strArr[0];
      address = strArr[1];
      phone = strArr[2];
      email = strArr[3];
   }
}

共有1个答案

花飞扬
2023-03-14

你有机会把你的输入文件转换成CSV吗?会更容易阅读和解析...

https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/

然后,您可以简单地读取一行,并将该车辆/汽车/卡车的所有数据/任何需要传递给构造函数的数据都包含在内。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VehicleParsing
{
    class Program
    {
        static void Main(string[] args)
        {
            // Will maintain the string in the current line
            string line;

            // Contains the vehicles
            List<Vehicle> vehicles = new List<Vehicle>();

            // Contains all properties associated with the current vehicle being looked at
            List<string> currentVehicleData = new List<string>();

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader("C:/Users/Timot/Desktop/vehiclesample.txt");
            while ((line = file.ReadLine()) != null)
            {
                // Indicates we have read in all properties of the current vehicle
                if(String.IsNullOrEmpty(line))
                {
                    string vehicleType = currentVehicleData[0];
                    switch (vehicleType)
                    {
                        case "foreign car":
                            vehicles.Add(new ForeignCar(currentVehicleData));
                            break;
                        case "truck":
                            vehicles.Add(new Truck(currentVehicleData));
                            break;
                        case "vehicle":
                            vehicles.Add(new Vehicle(currentVehicleData));
                            break;
                        case "bicycle":
                            vehicles.Add(new Bicycle(currentVehicleData));
                            break;
                        case "american car":
                            vehicles.Add(new AmericanCar(currentVehicleData));
                            break;
                        case "car":
                            vehicles.Add(new Car(currentVehicleData));
                            break;
                        default:
                            throw new NotImplementedException();
                    }

                    currentVehicleData = new List<string>();
                }

                // Indicates the current vehicle's data is still being read
                else
                {
                    currentVehicleData.Add(line);
                }
            }

            file.Close();
        }
    }
}
package com.company;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // Holds the list of all vehicles found in the file
        ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();

        // Holds the data associated with the current vehicle being read
        ArrayList<String> currentVehicleData = new ArrayList<String>();

        // Java knowledge lacking here - not certain why I can't simply let the FNF exception bubble up
        Scanner file = null;
        try {
            file = new Scanner(new File("C:/Users/Timot/Desktop/vehiclesample.txt"));
        } catch(FileNotFoundException fnfe) {
            // Do something with this exception
            return;
        }

        String line;
        while(file.hasNextLine()) {
            // Get next line
            line = file.nextLine();

            // Next line is empty, so currentVehicleData has all the data associated the current vehicle
            if(line.isEmpty()) {
                vehicles.add(GetVehicle(currentVehicleData));
                currentVehicleData = new ArrayList<String>();
            }

            // Still reading data for the current vehicle
            else {
                currentVehicleData.add(line);
            }
        }

        // Add final vehicle
        vehicles.add(GetVehicle(currentVehicleData));

        file.close();
    }

    public static Vehicle GetVehicle(ArrayList<String> properties)
    {
        String vehicleType = properties.get(0);
        switch(vehicleType) {
            case "foreign car":
                return new ForeignCar(properties);
            case "truck":
                 return new Truck(properties);
            case "vehicle":
                 return new Vehicle(properties);
            case "bicycle":
                 return new Bicycle(properties);
            case "american car":
                 return new AmericanCar(properties);
            case "car":
                 return new Car(properties);
            default:
                throw new NotImplementedException();
        }
    }
}
 类似资料:
  • 这是我到目前为止的代码: 这是我空的测试课。

  • 扫描有漏洞的车辆 为了找到有漏洞的车辆,你只需要在IP地址21.0.0.0/8 和 25.0.0.0/8 上扫描Sprint设备的端口6667。任何有响应的设备就是有漏洞的Uconect系统(或一个IRC服务器)。为了确定这一点,你可以尝试Telnet登陆这台设备并查找错误字符串“Unknown command”。 图-扫描设置 如果你想的话,接下来你可以与D-Bus服务交互,从而执行上述的任何操

  • 问题内容: 我刚刚开始学习Swift。我有要从文本文件读取的代码,应用程序显示了整个文本文件的内容。如何显示一行一行并多次调用该行? 包含以下内容: 以下是目前的情况。 如果还有另一种方法,请告诉我。将不胜感激。 问题答案: 斯威夫特3.0 该变量应该是数据的每一行。 使用的代码来自: 在用Obj-C编写的iOSSDK中逐行读取文件并使用NSString 查看旧版Swift的编辑历史记录。

  • 我刚刚开始学习 Swift。我已经从文本文件中读取了我的代码,并且应用程序显示整个文本文件的内容。如何逐行显示并多次调用该行? 包含以下内容: 以下是目前的… 如果有别的方法,请告诉我。非常感谢。

  • 问题内容: 请看下面的代码 在这里,首先获取文件的字节,然后将其写入文本文件。然后,我阅读了该文本文件,逐行阅读,并为每一行生成了一个单独的.txt文件。现在,原始程序被拆分为数千个文件。现在,我需要阅读所有.txt文件并重新生成.txt文件。我不知道怎么做最后一件事。我怎样才能做到这一点?请帮忙! 问题答案: 如果要操作任何类型的文件,请不要认为它们包含文本数据,而应将它们视为包含字节的二进制文

  • 问题内容: 下面的Mappers代码从HDFS读取文本文件正确吗?如果是这样的话: 如果不同节点中的两个映射器尝试几乎同时打开文件,会发生什么情况? 是否不需要关闭?如果是这样,如何在不关闭文件系统的情况下执行此操作? 我的代码是: 问题答案: 这将起作用,并进行一些修改-我假设您粘贴的代码被截断了: 您可以有多个映射器读取同一个文件,但是使用分布式缓存存在更多的局限性(不仅减少了承载文件块的数据