Question

So my code so far has tokenized the data, but it seems to be displaying the token as a full line of text instead of the individual substrings that I wish to get

My Code:

List<Flow> parseData(String filename) throws NumberFormatException, IOException
{
    List<Flow> fl = new ArrayList<Flow>();
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    String line = null;
    while ((line = reader.readLine()) != null) 
    {
            String data = line.replaceAll("->", "");
            String delim = "\t";
            StringTokenizer tok = new StringTokenizer(data, delim, true);

        while (tok.hasMoreTokens()) 
        {

            String token = tok.nextToken();
            System.out.println(token + "\n");
            String [] toKen = new String [9];

            if (delim.equals(token)) 
            {

                while(data.length() != 0)
                {
                    for (int i=0; i<9; i++)
                    {
                        toKen[i] = token;
                        System.out.println(toKen[i]);
                    }

                    double dur = Double.parseDouble(toKen[3]);
                    int pkts = Integer.parseInt(toKen[6]);
                    String Date = toKen[1].toString()+" "+toKen[2].toString();
                    Flow flow = new Flow(id,Date,dur,toKen[3],toKen[4],toKen[5],pkts,toKen[7],toKen[8]);
                    fl.add(flow);
//                      System.out.println(flow.toString());
                        id++;

                    }
            }
        }
    }

        return fl;

}

So its displaying the following as a single token:

2014-03-24 19:11:42.838  7611.668 UDP       192.168.0.15:5353       224.0.0.251:5353        53     5353    12

And i'd prefer tokens to look like this:

UDP
192.168.0.15:5353
224.0.0.251:5353
Was it helpful?

Solution

Try this:

String input = "2014-03-24 19:11:42.838  7611.668 UDP       192.168.0.15:5353       224.0.0.251:5353        53     5353    12";
String[] tokens = input.split("\\s+");
System.out.println(tokens[3]);
System.out.println(tokens[4]);
System.out.println(tokens[5]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top