Mamba
Mamba

Reputation: 1203

How to create a treemap from a text file

I have the following content of a txt file:

 Some text I would like not to inclcude

#id: col1, name, money\

  2017-01-02, Michael,  200 \
  2017-01-01, Tobias,   300 \
  2017-02-03, Susan,    400 \
  2017-05-04, John,     200 \
     ...     ...    ...
 Some text I would like not to inclcude      

I would like to create a Treemap and put col1 as my key:

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


    class earnings
    {
        public static void main(String[] args) throws FileNotFoundException {

            Scanner scanner = new Scanner(new FileReader("example.txt"));

            TreeMap<String, String> map = new TreeMap<String, String>();

            while (scanner.hasNextLine()) {
                String[] columns = scanner.nextLine().split("\t");
                map.put(columns[0],columns[0]);
            }

            System.out.println(map);
        }
    }

Out put with Some text I would like not to inclcude:

    {=,  2017-01-01,  Tobias,   300 \= 2017-01-01,  Tobias,   300 \,  
 2017-01-02, Michael,  200 \= 2017-01-02, Michael,  200 \,  2017-02-03,  
 Susan,    400 \= 2017-02-03,  Susan,    400 \,  2017-05-04,  John,     
 200 \= 2017-05-04,  John,     200 \,  Some text I would like not to 
 inclcude= Some text I would like not to inclcude, Some text I would 
 like not to inclcude=Some text I would like not to inclcude}

Out put without Some text I would like not to inclcude still has some duplication:

    {=,  2017-01-01,  Tobias,   300 \= 2017-01-01,  Tobias,   300 \,  
   2017-01-02, Michael,  200 \= 2017-01-02, Michael,  200 \,  2017-02-
  03,  Susan,    400 \= 2017-02-03,  Susan,    400 \,  2017-05-04,  
  John,     200 \= 2017-05-04,  John,     200 \}

How can I manage to create a treemap which has Some text I would like not to inclcude and looks as:

{ 2017-01-01, Tobias,  300 \ 2017-01-02, Michael, 200 \ 2017-02-03, Susan, 400 \  2017-05-04,  John,     200 \}

Or at least without duplication

Upvotes: 0

Views: 1351

Answers (2)

absin
absin

Reputation: 1166

String s = "2017-01-01, Tobias,   300 ";
String[] split = s.split(",");
if (split[0].trim().matches("\\d{4}-\\d{2}-\\d{2}")) {
    System.out.println(split[0].trim());
}

In your method, you can do:

while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split(",");
    if(columns[0].trim().matches("\\d{4}-\\d{2}-\\d{2}")        
    map.put(columns[0],columns[0]);
}

Upvotes: 0

Ian Mc
Ian Mc

Reputation: 5829

The reason you are getting unwanted data into the set is because your file input is complex, and you need processing rules to establish what parts of the file are valid. You have stated that you have a start trigger, and a stop trigger.

The states of processing are:

  1. Not Ready
  2. Ready (after reaching the start trigger) - In this state read into your Map
  3. Finished (after reaching the stop trigger)

It is not clear what value goes into the map (the key is the date). For now, I have concatenated the name and money value: Please fix this if this is not correct. Note you needed to split the String using a ",";

This code manages those states, and ensure that any data going into the Map is valid data (in between the start and stop triggers)

public class Earnings {

final static String START_TRIGGER = " Some text I would like not to inclcude";
final static String STOP_TRIGGER = " Some text I would like not to inclcude";

enum ProcessingState {
    NOT_READY,
    READY,
    FINISHED;
}

public static void main(String[] args) throws FileNotFoundException {

    Scanner scanner = new Scanner(new FileReader("Example.txt"));

    TreeMap<String, String> map = new TreeMap<String, String>();

    ProcessingState processingState = ProcessingState.NOT_READY;

    while (scanner.hasNextLine() && processingState != ProcessingState.FINISHED) {

        String lineToProcess = scanner.nextLine();

        if (processingState == ProcessingState.READY && lineToProcess.startsWith(STOP_TRIGGER))
            processingState = ProcessingState.FINISHED;

        if (processingState == ProcessingState.READY) {
            String[] columns = lineToProcess.split(",");
            map.put(columns[0],columns[1]+", "+columns[2]);
        }

        if (processingState == ProcessingState.NOT_READY && lineToProcess.startsWith(START_TRIGGER))
            processingState = ProcessingState.READY;


    }

    System.out.println(map);
}

}

I tested with your data, and this produced:

{  2017-01-01= Tobias,    300 \,   2017-01-02= Michael,   200 \,   2017-02-03= Susan,     400 \,   2017-05-04= John,      200 \}

Upvotes: 2

Related Questions