简体   繁体   English

如何计算导入到数组 CSV 文件中的行数?

[英]How do I count number of lines in an imported to array CSV file?

How do I count the number of lines in an imported to an array CSV file?如何计算导入到数组 CSV 文件中的行数? I have 5 lines in the csv.我在 csv 中有 5 行。 I've tried several methods, such as in the bufferedreader, but I couldn't get it to work.我已经尝试了几种方法,例如在 bufferedreader 中,但我无法让它工作。 I tried using a loop to iterate through the array, but doesn't recognize the menu array.我尝试使用循环遍历数组,但无法识别菜单数组。 can you please help shed some light on this.你能帮我解释一下吗? thanks in advance.提前致谢。

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;
        }

    }

}

Just increment lines in your while loop:只需在 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

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

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