0

I have a csv file with values like these:

1/1/1983;1,7;-3;8;-0,7;84;4;2;11;0;1030;0;0
2/1/1983;2,7;-2;8,4;1,9;94;2;2;15;0;1027;0;0
3/1/1983;4,1;-0,4;11,3;3,1;93;3;3;13;0;1030;0;0
4/1/1983;7,6;1,3;15;5,1;84;9;8;28;0;1027;0;0
5/1/1983;5,6;1,4;10;5,1;97;2;2;11;0;1023;0;0
6/1/1983;7;5,5;7,5;7;100;1;3;9;0;1028;0;0
7/1/1983;7,7;5;13,4;7,1;96;1;4;20;0;1029;0;0
8/1/1983;7,9;7;15,5;7,4;97;2;7;24;0;1029;0;1
9/1/1983;6,7;1;10,3;4,1;83;8;15;44;0;1033;0;0,3
10/1/1983;2,2;-1,9;8;0,4;88;8;4;13;0;1036;0;0
11/1/1983;0,7;-3,4;6,4;-1,2;87;3;1;13;0;1038;0;0
12/1/1983;0,2;-4,7;8;-1,7;87;6;4;9;0;1037;0;0
13/1/1983;1,7;-5,2;11,1;-0,1;88;4;3;15;0;1032;0;

So i have found on a website a Csv Parser implementation:

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

public class ScannerExample 
{
    public static void main(String[] args) throws FileNotFoundException 
    {
        //Array
        ArrayList<String> weather = new ArrayList<String>();
        //Get scanner instance
        Scanner scanner = new Scanner(new File("C:/csv/meteo2.csv"));

        //Set the delimiter used in file
        scanner.useDelimiter(";");

        //Get all tokens and store them in some data structure
        //I am just printing them
        while (scanner.hasNext()) 
        {
            weather.add(scanner.next());
        }
         System.out.println(weather.get(12));
        //Do not forget to close the scanner  
        scanner.close();
    }
}

But I Have a problem with the last element of one line and the first element of the successive line : Infact when in the code i try to print the twelfth element (that must be 0). It prints

0
2/1/1983

But it's considered as only one element. There is a solution to that?

1
  • You can read the file by lines (scanner.nextLine()) and then split the induvidual lines. Commented Sep 24, 2013 at 10:33

1 Answer 1

4

If you want the semicolon and the linebreak as a delimiter, you should use

scanner.useDelimiter("[;\n]");

as Scanner#useDelimiter(String pattern) expects a regular expression as the parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry, it works but after the last element (the 0) remain a space like above, altough now the subsequent date is correctly another element of the list. How can i solve?
@Markviduka: Sorry, I don't understand what you mean.
Above i have this string "0 (newline) 2/1/1983". With your code i solved the problem, and now, "2/1/1983" it's a new string. However, the "0" string have the same problem of above and it's "0 (newline) " . Hope you understand
@Markviduka: No, when \n is one of the delimiters, it won't be part of a token. You probably insert the newline character when printing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.