mfmz
mfmz

Reputation: 227

Read and compare last token of each line using Scanner

I have a running code which reads a csv file line by line and append each line with new info. But I need to compare the last token of each line. If the last token is not equal to the last token of previous line, I want to increment the appended leading label by 1.

Or to make things easier to understand. The String 'label' = last token of the line.

    StringBuffer sb = new StringBuffer();
    BufferedReader br = new BufferedReader(new FileReader(filename));

    String line = br.readLine();
    Scanner scanner;

    while (line != null) {
        sb.append(label);
        int indexCount = 1;
        scanner = new Scanner(line);
        scanner.useDelimiter(",");

        while (scanner.hasNext()) {

            sb.append(" " + scanner.next() + ":" + indexCount);

        }
        sb.append(System.getProperty("line.separator"));
        line = br.readLine();
    }

My current output is this :

1 0.24887:1 0.64811:1 0.5987:1 0.39418:1 0.96313:1 0.92772:1 1:1
1 0.59092:1 0.66222:1 0.6655:1 0.45021:1 0.87868:1 0.92897:1 1:1
1 0.67556:1 0.66291:1 0.43898:1 0.37441:1 0.95397:1 0.92851:1 1:1
1 0.70637:1 0.66395:1 0.859:1 0.39656:1 0.96331:1 0.93005:1 2:1
1 0.93686:1 0.66841:1 0.66013:1 0.39382:1 0.96343:1 0.92674:1 2:1
1 0.1355:1 0.66448:1 0.15662:1 0.39256:1 0.98848:1 0.90344:1 2:1

What I want to achieve is this:

1 0.24887:1 0.64811:1 0.5987:1 0.39418:1 0.96313:1 0.92772:1 1:1
1 0.59092:1 0.66222:1 0.6655:1 0.45021:1 0.87868:1 0.92897:1 1:1
1 0.67556:1 0.66291:1 0.43898:1 0.37441:1 0.95397:1 0.92851:1 1:1
2 0.70637:1 0.66395:1 0.859:1 0.39656:1 0.96331:1 0.93005:1 2:1
2 0.93686:1 0.66841:1 0.66013:1 0.39382:1 0.96343:1 0.92674:1 2:1
2 0.1355:1 0.66448:1 0.15662:1 0.39256:1 0.98848:1 0.90344:1 2:1

How can I achieve this?

Upvotes: 0

Views: 1026

Answers (2)

Kick Buttowski
Kick Buttowski

Reputation: 6739

As far as, I was able to understand what you want to extract the last part of each line.

For example:

1 0.24887:1 0.64811:1 0.5987:1 0.39418:1 0.96313:1 0.92772:1 1:1

you want to have 1:1

and after that 1 and 1 get spearted

The only way that come to my mind is using split function

Read about split function

so you can do the following

    String s = "1 0.24887:1 0.64811:1 0.5987:1 0.39418:1 0.96313:1 0.92772:1 1:1";
     String[] sp = s.split(" ");
     String[] parsedLastToken = sp[sp.length-1].split(":");
     for (int i = 0; i < parsedLastToken.length; i++) {
          System.out.println(parsedLastToken[i]); 
    }

output:

1
1

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

StringBuilder objects are mutable, and you can insert text into them anywhere, including their beginning. Check the StringBuilder API and look for the many variants of the insert(...) method. Another possible solution: to create an object from each row of data, and give this class a method to allow proper visual presentation of the object's state.

And use a new StringBuilder for each line will also make this easier, and then add the result to a master StringBuilder.

For example:

  StringBuilder masterSb = new StringBuilder();
  BufferedReader br = new BufferedReader(new FileReader(filename));
  String line = br.readLine();
  Scanner scanner;
  while (line != null) {
     StringBuilder sb = new StringBuilder();
     // *** sb.append(label); // *** not sure what this is for
     int indexCount = 1;
     scanner = new Scanner(line);
     scanner.useDelimiter(",");
     String lastToken = "";
     while (scanner.hasNext()) {
        lastToken = scanner.next();
        sb.append(" " + lastToken + ":" + indexCount);
     }
     sb.insert(0, lastToken + " "); // **** here we pre-pend the last token  
     sb.append(System.getProperty("line.separator"));
     line = br.readLine();
     scanner.close(); // ***** don't forget to close your Scanner
     masterSb.append(sb.toString());  // ***** add line to master SB
  }

Caveat: code not tested

Upvotes: 1

Related Questions