0

I want to return the part of each line that contains a dot between the numbers which is the GPA portion of each line from a text file.

I can figure out how to read each line in the text file and print it. I have tried using .contains and .substring to try to capture the portion I want from each line but I do not know how to do so.

1.wita7557 | william | tang | male | 3.0 | counselor | 5 | database | leader  
2.tosm5791 | tommy | smith | male | 3.5 | mastermind | 5 | designer | leader  
3.drco5905 | drew | collins | male | 3.7 | inspector | 3 | designer | programmer 

so from the above code I would want an expected result into an array such as [3.0,3.5,3.7]

Bentaye
  • 9,403
  • 5
  • 32
  • 45
  • Split your line at each `|` using `line.split("\\|")` and get your desierd string at the 4th index of the resulting array. – Eritrean Nov 08 '19 at 11:01
  • You need to google 3 things: How to read a file line by line? How to split a string on pipe character? How to trim a String? – Bentaye Nov 08 '19 at 12:38
  • Do you need a list of Strings, or a list of Doubles? – Bentaye Nov 08 '19 at 12:41

4 Answers4

0

the below should give you the 5th element from each line (split by '|' ) :

line.split("\\|")[4] 
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

Example code

Double.valueOf(
"2.tosm5791 | tommy | smith | male | 3.5 | mastermind | 5 | designer | leader"
.split("\\|")[4])
lczapski
  • 4,026
  • 3
  • 16
  • 32
0

Complete program:

public static void main(String[] args) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader("C:/Users/user/Desktop/input.txt"));
        List<Double> doubleList = new ArrayList<>();
        String buff[];
        String line = reader.readLine();
        while (line != null) {
            buff = line.split("\\|");           
            doubleList.add(Double.parseDouble(buff[4]));
            line = reader.readLine();
        }
        reader.close();
        System.out.println(doubleList);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
AbhiN
  • 642
  • 4
  • 18
0

What you want to do is:

This can be done using a single lambda this way:

List<Double> list = Files.lines(Paths.get("myFile.txt")) // real all lines
    .map(line -> line.split("\\|"))                      // split each line on the | character
    .map(array -> array[4])                              // take the 5th element (index 4)
    .mapToDouble(Double::valueOf)                        // parse them into doubles
    .boxed()                                             // box them to allow collection
    .collect(Collectors.toList());                       // make a list out of them

Or same thing without lambdas:

List<String> lines = Files.readAllLines(Paths.get("myFile.txt"));
List<Double> list = new ArrayList<>();
String[] array;
String stringValue;
Double doubleValue;
for(String line : lines) {
    array = line.split("\\|");
    stringValue = array[4];
    doubleValue = Double.valueOf(stringValue);
    list.add(doubleValue);
}

Note: Files.line() throws an IOException so you will need to write that code into a try/catch/finally block

Bentaye
  • 9,403
  • 5
  • 32
  • 45