【发布时间】: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