简体   繁体   English

为什么我的方法只能读取一行文本?

[英]Why is my method only reading one line of text?

I have a method that would read parts of a text file that has 4 parts: date, name, description and amount like 我有一种方法可以读取文本文件的一部分,该文件有4个部分:日期,名称,描述和金额,例如

4/5/2018, gel, hair product, 20.00
4/4/2018, wax, hair product, 20.00

and so on... 等等...

My problem is that my method would only read the 1st line and then output my catch method saying that the file isn't found. 我的问题是我的方法只会读取第一行,然后输出我的catch方法,说找不到该文件。

public static void showRecordedExpense(String filename)throws IOException {
    String date = "";
    String name = "";
    String description = "";
    double amount = 0.00;
     try{
         Scanner read = new Scanner(new File(filename));
         while (read.hasNextLine()){
             String oneLine = read.nextLine();
             String[] parts = oneLine.split(",");
             try {
                 date = parts[0];
                 name = parts[1];
                 description = parts[2];
                 amount = Double.parseDouble(parts[3]);
                 System.out.printf("%15s%15s%15s%20s%n", "---------------", "---------------",
                         "---------------", "---------------------");
                 System.out.printf("%15s%15s%15s%31s%n","Date", "Name", "Description","Amount");
                 System.out.printf("%15s%14s%33s%15s%n",date,name,description,amount);
                 System.out.printf("%15s%15s%15s%20s%n", "---------------", "---------------",
                         "---------------", "---------------------");
             }catch (Exception e){
                 System.out.println("no");
             } finally {
                 read.close();
             }
         }
     }catch (Exception e){
         System.out.println("The file could not be found");
     }

}

EDIT: Taking out the finally worked. 编辑:取出最后的工作。

Read here for details on how finally works. 在这里阅读有关finally工作原理的详细信息。 You are currently closing your Scanner at the end of your first iteration of the while loop due to the finally you paired with your try/catch . 由于您finallytry/catch配对,因此您目前正在while循环的第一次迭代结束时关闭Scanner。 The next iteration of the while can no longer read from the file since you closed it, which is why it's only reading the first line. 自关闭文件以来, while的下一次迭代无法再从文件中读取,这就是为什么它仅读取第一行的原因。 Consider taking out the finally and just closing the Scanner once the while loop is complete. 考虑在while循环完成后取出finally并仅关闭扫描仪。

     try{
         Scanner read = new Scanner(new File(filename));
         while (read.hasNextLine()){
             String oneLine = read.nextLine();
             String[] parts = oneLine.split(",");
             try {
                 date = parts[0];
                 name = parts[1];
                 description = parts[2];
                 amount = Double.parseDouble(parts[3]);
                 System.out.printf("%15s%15s%15s%20s%n", "---------------", "---------------",
                         "---------------", "---------------------");
                 System.out.printf("%15s%15s%15s%31s%n","Date", "Name", "Description","Amount");
                 System.out.printf("%15s%14s%33s%15s%n",date,name,description,amount);
                 System.out.printf("%15s%15s%15s%20s%n", "---------------", "---------------",
                         "---------------", "---------------------");
             }catch (Exception e){
                 System.out.println("no");
             }
         }

         read.close();
     }catch (Exception e){
         System.out.println("The file could not be found");
     }

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

相关问题 为什么我的Java程序只读取一行输入? - Why is my Java program only reading one line of input? 为什么我的程序读取的行数比实际少一行? 为什么我的数组只接收一个? - Why is my program reading one less line than there actually is? And why is my array taking in only ones? InputstreamReader 只读取一行? - InputstreamReader only reading one line? 为什么我的方法只返回存在于我的 ArrayList 中的重复项之一? - Why is my method only returning one of the duplicates that exist in my ArrayList? 链接列表程序从文本文件中读取行时仅添加一个对象 - Linked List program only adding one object when reading line from text file 需要程序方面的帮助,FileInputStream只能读取文本文件的一行时遇到麻烦。 [作业帮助] - Need assistance with program, having trouble with FileInputStream only reading one line of a text file. [HomeworkHelp] 为什么在SWT Eclipse(Text)中给我打印一行。 我想在我的文本中打印多行 - Why printed me one line in SWT Eclipse (Text). I wanted to print multi line in my Text 为什么文本显示在一行? - Why the text is displayed on one line? 为什么我的DataInputStream只读取114个字节? - Why is my DataInputStream only reading 114 bytes? 我的bufferedread只读取文件的第一行吗? - My bufferedread only reading the first line of my file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM