可能由空白字符引起的显示数组问题

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


空白字符 我的代码显示此结果:


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;


    }



DIEA
浏览 103回答 2
2回答

收到一只叮咚

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

慕盖茨4494581

在 StringBuilder 中,您收集所有行:AG53DBO,Mercedes,1000,(255:0:0),4\r\nMD17WBW,Volkswagen,2300,(0:0:255),5\r\n...这个字符串应该首先被吐上ls- 然后你会有用逗号分隔的字段的行。现在只需用逗号分割就会导致数组元素加倍4\r\nMD17WBW。就像是:String fileName =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "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) {&nbsp; &nbsp; String[] data = line.split(",");&nbsp; &nbsp; Car car = new Car(data);&nbsp; &nbsp; cars.add(car);}Path、Paths 尤其是Files是非常方便的类。使用 java Streams 还可以缩写如下:String fileName =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "C:\\Users\\beng\\eclipse-workspace\\Assignment Trailblazer\\Car Data";Path path = Paths.get(fileName);List<Car> cars = Files.lines(path)&nbsp; &nbsp; &nbsp;// Stream<String>&nbsp; &nbsp; .map(line -> line.split(","))&nbsp; &nbsp; &nbsp; // Stream<String[]>&nbsp; &nbsp; .map(Car::new)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Stream<Car>&nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; &nbsp;// List<Car>这里.lines返回Stream<String>文件中的行(游标),没有行分隔符。然后.map(l -> l.split(","))分割每一行。然后在Car(String[])字符串数组上调用构造函数。然后将结果收集在一个列表中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java