-1

In each line of the file provided, each line follows this structure:

8 numbers then 1 comma and then 2 numbers.

For example: 98468631,51

I would like to only use the two digits after the comma.

Here is the program:

import java.io.*;
import java.util.regex.*;

public class Read {

public static void main(String[] args) {
    String[] marks = new String[100];

    File file = new File("sd100-marks.csv");

    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));

        for(int i = 0; i < 100; i++) {
            try {
                String line = reader.readLine();
                String[] sp = line.split(",");
                line = sp[0];
                marks[i] = line;
            } catch (IOException e) {
                System.out.println("Could not read!");
            }
        }


    } catch (FileNotFoundException e) {
        System.out.println(e);
    }
    for(int i = 0; i < 100; i++) {
        System.out.println(marks[i]);
    }

  }

}

Basically, I am not sure what regular expression to use in the split() method. For now, I have "," passed in to the method, but that is not useful for what I am trying to do and simply displays all of the numbers before the comma.

Stephen-Wisniewski
  • 321
  • 1
  • 2
  • 13

5 Answers5

2

Sring.split is indeed the method to use.

String[] sp = line.split(",");
//sp[0] contains the first 8 digits
//sp[1] contains the part after the ",".

So use :

line = sp[1];//instead of sp[0]
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
1

split method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The string "boo:and:foo", for example, yields the following results with these expressions:

Regex   Result
:   { "boo", "and", "foo" }
o   { "b", "", ":and:f" }

Have a try;

String line = "98468631,51";
String[] sp = line.split(",");
System.out.println(sp[0]);//98468631
System.out.println(sp[1]);//51

Split a String Q&A

Source

Community
  • 1
  • 1
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
1

Important note: don't use String.split() in a for loop.

It's more efficient to use Pattern/Matcher in situations like this.

String[] marks = new String[100];
Pattern pattern = Pattern.compile(","); // <== this
File file = new File("sd100-marks.csv");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
    for (int i = 0; i < 100; i++) {
        marks[i] = pattern.split(reader.readLine())[1]; // we need the part after the comma i.e. index = 1
    }
} catch (IOException e) {
    e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
    System.out.println(marks[i]);
}
FaNaJ
  • 1,329
  • 1
  • 16
  • 39
0

This seems to be wrong for what you are trying to do as you are getting the first part:

line = sp[0];

Try getting the second part and you should also check that there are 2 parts in sp:

if (sp.length > 1) {
    line = sp[1];
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
0

Following regex can be used and the group could be extracted if you really want to use regex: ([0-9]),([0-9]) Then group(2) would give you the numbers after the comma. You can refer this documentation for more details.

Prashant
  • 4,775
  • 3
  • 28
  • 47