导入txt文件时,有没有办法指定格式化的方式?

时间:2017-09-09 15:22:01

标签: java arrays text-files

input.txt我的文本文件存储在数组中,所以我总是可以使用myArray.length< x或myArray.length> x退出系统。然而,这似乎并没有奏效。还有另一种我不知道的方法可以让用户知道文本文件输入在崩溃或退出程序之前是无效的吗?

    800 1000 800
    450 845 1200
    1800 250 400
    0 1500 1800
    600 500 1000
    700 1400 1700
    675 400 900

String file = "input.txt";
String text = "";
double [] breakfast = new double [7];
double [] lunch = new double [7];
double [] dinner = new double [7];
try {
    Scanner s = new Scanner(new File(file));
    for (int i = 0;i<7;i++) {
        breakfast[i] = s.nextInt();
        lunch[i] = s.nextInt();
        dinner[i] = s.nextInt();
        if (breakfast.length == 0 || breakfast.length > 7 || breakfast.length < 7) {
            System.out.println("Your file will not work with this program. Please select another.");
            System.exit(0);
        }

text file

0 个答案:

没有答案