gpt4 book ai didi

java - 在 Java 中使用扫描器

转载 作者:行者123 更新时间:2023-11-29 05:25:05 25 4
gpt4 key购买 nike

编写一个读取 n.csv 文件并创建对象“Account”的类。这是一个测试。

代码:

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class AccountTest {

public static void main(String[] args) throws FileNotFoundException {
File file = new File("1234567890989.csv");
Scanner scanner;
try {
scanner = new Scanner(file);

while(scanner.hasNext()) {
scanner.nextLine();
scanner.next("Account");
String pp = scanner.next();

System.out.println(pp);

}


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

文件:

Account Info - 14-03-2014
Account ;1234567890989 ; EUR ;POUPANCA ;SavingsAccount ;
Start Date ;31-10-2013
End Date ;03-01-2014
Date ;Value Date ;Description ;Draft ;Credit ;Accounting balance ;Available balance
31-10-2013 ;31-10-2013 ;SUMMARY ;0.0 ;200.0 ;2600.0 ;2600.0
30-11-2013 ;30-11-2013 ;SUMMARY ;0.0 ;200.0 ;2800.0 ;2800.0
31-12-2013 ;31-12-2013 ;SUMMARY ;0.0 ;200.0 ;3000.0 ;3000.0
02-01-2014 ;02-01-2014 ;TRANSF ;0.0 ;300.0 ;3300.0 ;3300.0
02-01-2014 ;02-02-2014 ;TRANSF ;0.0 ;300.0 ;3600.0 ;3600.0
03-01-2014 ;03-01-2014 ;TRANSF ;0.0 ;300.0 ;3900.0 ;3900.0

返回 ';1234567890989' 但我只想拥有 '1234567890989' 并将其格式化为 Long。

最佳答案

如果我没理解错的话,这应该可行 -

if (scanner.hasNextLine()) {            // Check for the line.
scanner.nextLine(); // read the line.
if (scanner.hasNext("Account")) { // Check for account.
scanner.next("Account"); // read the field.
if (scanner.hasNext()) { // Check if there is more.
String pp = scanner.next(); // read it.
if (pp.startsWith(";")) { // remove a leading ';'
pp = pp.substring(1);
}
long lpp = Long.parseLong(pp); // convert to long.
System.out.println(lpp); // print it.
}
}
}

输出是

1234567890989

关于java - 在 Java 中使用扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993407/

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