簡體   English   中英

如何計算導入到數組 CSV 文件中的行數?

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

如何計算導入到數組 CSV 文件中的行數? 我在 csv 中有 5 行。 我已經嘗試了幾種方法,例如在 bufferedreader 中,但我無法讓它工作。 我嘗試使用循環遍歷數組,但無法識別菜單數組。 你能幫我解釋一下嗎? 提前致謝。

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 循環中增加行:

        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