Question

Writing a class that reads n .csv file and creates the objects "Account". This is a test.

Code:

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();
        }

    }

}    

File:

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

Returns ';1234567890989' but I want to have only '1234567890989' and format it to Long.

Was it helpful?

Solution

If I understand you correctly, this should work -

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.
        }
    }
}

Output is

1234567890989

OTHER TIPS

Try this:

...
String pp = scanner.next();
String qq;

qq = StringUtils.strip(StringUtils.trim(pp), ";");

StringUtils is part of Apache Commons Lang

strip removes characters from the start and end of the string, so be sure to trim whitespace before calling strip.

By default, Scanner uses whitespace as a delimiter. Thus, Scanner.next is looking at all those spaces in your file and separating your values by that, and thus you're capturing the semicolons.

You can change the delimiter being used with the useDelimiter method. Something along these lines should work:

Scanner scanner = new Scanner(new File("1234567890989.csv")).useDelimiter(";");
while (scanner.hasNext())
{
    String token = scanner.next();
    ...
}

To transform your token into a long, you'll have to decide whether the token should be a long, and then use Long.parseLong(token).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top