显示数组问题可能是由于空白字符

display array issues possibly due to whitespace characters

我正在尝试导入一个包含汽车信息的 txt 文件,并将字符串分隔成数组,然后显示它们。门的数量与下一个车牌相结合。已经尝试了几种方法来消除我认为导致问题但没有成功的空白字符。

whitespace chars 我的代码显示此结果:

Number Plate : AG53DBO
 Car Type : Mercedes
 Engine Size : 1000
 Colour : (255:0:0)
 No. of Doors : 4
MD17WBW

Number Plate : 4
MD17WBW
 Car Type : Volkswagen
 Engine Size : 2300
 Colour : (0:0:255)
 No. of Doors : 5
ED03HSH

代码:

public class Application {

    public static void main(String[] args) throws IOException {

        ///// ---- Import File ---- /////

        String fileName =
                "C:\Users\beng\eclipse-workspace\Assignment Trailblazer\Car Data";

        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        String ls = System.getProperty("line.separator");
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
            }
        reader.close();

        String content = stringBuilder.toString();

        ///// ---- Split file into array ---- /////
        String[] dataList = content.split(",");

        // Display array
        for (String temp : dataList) {
//          System.out.println(temp);
        }

        ArrayList<Car> carArray = new ArrayList();

        // Loop variables
        int listLength = 1;
        int arrayPosition = 0;
        // (dataList.length/5)
        while (listLength < 5) {
            Car y = new Car(dataList, arrayPosition);
            carArray.add(y);
            listLength++;
            arrayPosition += 4;
        }

        for (Car temp : carArray) {
            System.out.println(temp.displayCar());
        }
    }
}

public class Car {

    String[] data;

    private String modelUnpro;
    private String engineSizeUnpro;
    private String registrationUnpro;
    private String colourUnpro;
    private String doorNoUnpro;

    // Constructor
    public Car(String[] data, int arrayPosition) {
        registrationUnpro = data[arrayPosition];
        modelUnpro = data[arrayPosition + 1];
        engineSizeUnpro = data[arrayPosition + 2];
        colourUnpro = data[arrayPosition + 3];
        doorNoUnpro = data[arrayPosition + 4];
    }

    // Getters
    private String getModelUnpro() {
        return modelUnpro;
    }
    private String getEngineSizeUnpro() {
        return engineSizeUnpro;
    }
    private String getRegistrationUnpro() {
        return registrationUnpro;
    }
    private String getColourUnpro() {
        return colourUnpro;
    }
    private String getDoorNoUnpro() {
        return doorNoUnpro;
    }

    public String displayCar() {
        return "Number Plate : " + getRegistrationUnpro() + "\n Car Type : " + getModelUnpro() + "\n Engine Size : "
                + getEngineSizeUnpro() + "\n Colour : " + getColourUnpro() + "\n No. of Doors : " + getDoorNoUnpro() + "\n";
    }

}

文本文件:

AG53DBO,Mercedes,1000,(255:0:0),4
MD17WBW,Volkswagen,2300,(0:0:255),5
ED03HSH,Toyota,2000,(0:0:255),4
OH01AYO,Honda,1300,(0:255:0),3
WE07CND,Nissan,2000,(0:255:0),3
NF02FMC,Mercedes,1200,(0:0:255),5
PM16DNO,Volkswagen,1300,(255:0:0),5
MA53OKB,Honda,1400,(0:0:0),4
VV64BHH,Honda,1600,(0:0:255),5
ER53EVW,Ford,2000,(0:0:255),3

从 while 循环中删除行分隔符。

        String fileName = "D:\Files\a.txt";
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line.trim());
        }
        reader.close();
        String content = stringBuilder.toString();
        String[] dataList = content.split(",");
        ArrayList<Car> carArray = new ArrayList();
        int listLength = 1;
        int arrayPosition = 0;
        // (dataList.length/5)
        while (listLength < 3) {
            Car y = new Car(dataList, arrayPosition);
            carArray.add(y);
            listLength++;
            arrayPosition += 4;
        }
        for (Car temp : carArray) {
            System.out.println(temp.displayCar());
        }

在 StringBuilder 中收集所有行:

AG53DBO,Mercedes,1000,(255:0:0),4\r\nMD17WBW,Volkswagen,2300,(0:0:255),5\r\n...

这个字符串应该首先吐在 ls - 然后你有用逗号分隔的字段的行。

现在仅用逗号分隔将导致数组元素加倍 4\r\nMD17WBW

类似于:

String fileName =
            "C:\Users\beng\eclipse-workspace\Assignment Trailblazer\Car Data";
Path path = Paths.get(fileName);
List<String> lines = Files.readAllLines(path); // Without line ending.

List<Car> cars = new ArrayList<>();
for (String line : lines) {
    String[] data = line.split(",");
    Car car = new Car(data);
    cars.add(car);
}

路径,路径,尤其是 Files 非常方便 类。使用 java Streams 也可以缩写为:

String fileName =
            "C:\Users\beng\eclipse-workspace\Assignment Trailblazer\Car Data";
Path path = Paths.get(fileName);
List<Car> cars = Files.lines(path)     // Stream<String>
    .map(line -> line.split(","))      // Stream<String[]>
    .map(Car::new)                     // Stream<Car>
    .collect(Collectors.toList());     // List<Car>
  1. 此处 .lines returns 文件中的 Stream<String>(移动光标)行,没有行分隔符。
  2. 然后.map(l -> l.split(","))分割每一行。
  3. 然后在字符串数组上调用 Car(String[]) 构造函数。
  4. 然后将结果收集到列表中。