Robin
Robin

Reputation: 55

Java - Split String using regex and new line

I have a file that I scan into my program and store in a String using this code:

    try {
        data= new Scanner(new File("file.csv")).useDelimiter("\\Z").next();
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    }

The file.csv looks something like this:

"RowA";"RowB"
55;56
57;58
59;60
61;62

Now, I'm trying to extract each number and put them in a String[] like so:

    String[] number= data.split(";|\\r?\\n|\"|[a-zA-Z]");

When I print the code like so:

    for(int i = 0; i < number.length; i++){
        System.out.println("Line: " + number[i]);
    }

I get the following output:

Line: 
Line:  
Line:  
Line:  
Line:  
Line:  
Line: 
Line: 
Line: 
Line: 
Line: 
Line: 
Line: 
Line: 
Line: 55
Line: 56
Line: 57
Line: 58
Line: 59
Line: 60
Line: 61
Line: 62

Why are the first indexes in the array blank and how can I remove it?

Thank you.

Upvotes: 1

Views: 640

Answers (3)

explv
explv

Reputation: 2759

For a solution to your regex see the answer by @Hackerdarshi.

However, I propose an alternative method, which is likely more efficient to parse the numbers.

Instead of reading the whole file into a String and then using Regex to parse the numbers, you can instead read the file line by line, split each line on ";" and then parse each number returned by the split:

List<Integer> numbers = new ArrayList<>();

File file = new File("file.csv");

try (FileReader fileReader = new FileReader(file);
     BufferedReader bufferedReader = new BufferedReader(fileReader)){
    bufferedReader.readLine();    

    String line;
    while ((line = bufferedReader.readLine()) != null) {
        for (final String number : line.split(";")) {
            numbers.add(Integer.parseInt(number));
        }
    }
} catch(final IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Julian
Julian

Reputation: 192

If you are sure that you want an element for every group of consecutive digits a quick and easy solutions would be:

String[] number= data.split("([^0-9])+");

This will provide your expected output as long as every value you expect is an integer (no decimal separator) and that there is no digits anywhere else.

EDIT: If the first/last char of data is not a digit it will add one empty item at the start/end of the number array.

Upvotes: 0

dryairship
dryairship

Reputation: 6077

In this regex :

;|\r?\n|"|[a-zA-Z]
  • " matches the double quotes in the String ("RowA";"RowB")
  • [a-zA-Z] matches each character in "RowA";"RowB".

Hence it is split at all those places and you get the blanks.

You can remove these parts if you don't need them, using:

String[] number= data.split(";|\\r?\\n");

I can also see that you want only numbers in your data, and not " and characters. In that case, you can replace the " and characters by using replaceAll(), before you split it.

data.replaceAll("\"|[a-zA-Z]",""); 

Upvotes: 2

Related Questions