编写一个读取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

09-11 19:39