【问题标题】:How do I count number of lines in an imported to array CSV file?如何计算导入到数组 CSV 文件中的行数?
【发布时间】:2020-11-09 08:30:02
【问题描述】:

如何计算导入到数组 CSV 文件中的行数? 我在 csv 中有 5 行。我尝试了几种方法,例如在缓冲阅读器中,但我无法让它工作。我尝试使用循环遍历数组,但无法识别菜单数组。 你能帮我解释一下吗?提前致谢。

package carrentalsystem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class MenuDisplay {

    public static void displayCarList() throws FileNotFoundException, IOException {

        String path = "carlist.csv";
        long lines = 0;

        String line = "";

        System.out.println("***************************************************************");
        System.out.println("       Welcome to the Carrington Car Rental");
        System.out.println("***************************************************************");
        System.out.println("Cars available for booking");
        System.out.println("______________________________________________________________");

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));

            while ((line = br.readLine()) != null) {
                String[] menu = line.split(",");
                System.out.println(menu[0] + "\t" + menu[1] + "\t" + menu[2] + "\t" + menu[3] + "\t" + menu[4] + "\t" + menu[5]);
            }

        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }

        System.out.println("______________________________________________________________");
        System.out.println("Total of" + "cars available." + "\n");

        System.out.println("* Note for premium cars, there's addition 5% insurance" + " access applied to the car rate");
        System.out.println("*************************************************");
        System.out.println("\n");
        getSelection();

    }

    public static void getSelection() {
        Scanner scan = new Scanner(System. in );
        System.out.println("Select from the following options." + "\n");
        System.out.println("1. To make a booking");
        System.out.println("2. To exit the System");
        System.out.println("\n");
        System.out.println("Enter your selection: ");
        int selection = scan.nextInt();
        CarAndBookingDates b;

        switch (selection) {
        case 1:
            b = new CarAndBookingDates();
            b.carSelection();
            b.promptForYear();
            b.promptForMonth();
            b.promptForDay();
            b.getCarBookingDateFull();
            break;
        case 2:
            System.out.println("Good Bye!");
            System.exit(0);
            break;
        default:
            System.out.println("INVALID");
            break;
        }

    }

}

【问题讨论】:

  • 为什么不在你的while循环中增加lines
  • 喜欢lines++,这就是绿巨人的意思
  • @Hulk 你能指导我吗,因为我无法掌握这些线++迭代。

标签: java


【解决方案1】:

只需在你的 while 循环中增加行数:

        while ((line = br.readLine()) != null) {
            lines++;
            String[] menu = line.split(",");
            System.out.println(menu[0] + "\t" + menu[1] + "\t" + menu[2] + "\t" + menu[3] + "\t" + menu[4] + "\t" + menu[5]);
        }

// Now "lines" is the number of lines read from the file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多