gpt4 book ai didi

java - 在 Java 中读取文本文件的特定行

转载 作者:行者123 更新时间:2023-11-29 06:55:51 24 4
gpt4 key购买 nike

我是 Java 新手。我正在尝试在文本文件上做一个数据库(不要问我为什么)。我想做的是读取文本文件的特定行。

文本文件:

Salary
3,400
21/12/2015
Tax Refund
3
22/12/2015
Tax Refund
320
23/12/2015
Savings Deposit
1,230
23/12/2015
Bonus
343
23/12/2015

每 3 行有一个新条目。例如,Salary 是类别,3,400 是金额,21/12/2015 是日期。我想做的是只读取金额(例如 3,400 3 320 1,230 等)。我唯一成功做的就是通过在真正的打印语句上匹配它们来打印行,使用以下代码:

while(opnEsoda.hasNext()) { // diabazei kathe seira sto txt
// diabazei kathe seira kai emfanizei to value tis kathe mias ana 3.
System.out.print("You got money from: " + opnEsoda.nextLine()+ ", ");

System.out.print("Amount: " + opnEsoda.nextLine()+", ");
System.out.print("Date: " + opnEsoda.nextLine()+". ");
System.out.println();
}

opnEsoda 是扫描器并打印:

You got money from: Salary, Amount: 3,400, Date: 21/12/2015. 
You got money from: Tax Refund, Amount: 3, Date: 22/12/2015.
You got money from: Tax Refund, Amount: 320, Date: 23/12/2015.
You got money from: Savings Deposit, Amount: 1,230, Date: 23/12/2015.
You got money from: Bonus, Amount: 343, Date: 23/12/2015.

最佳答案

只需跳过其他行:-)

while(opnEsoda.hasNext()) {
// skip category
opnEsoda.nextLine();

System.out.print("Amount: " + opnEsoda.nextLine()+", ");

// skip date
opnEsoda.nextLine();

System.out.println();
}

关于java - 在 Java 中读取文本文件的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34432014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com