-1

The input is String array as below,

{"1112323 400 error","1112323 400 error","9988778 400 error"}

I need to print the timestamp i.e the number at the start of the sentences and its frequency throughout the array

I've come only this far as of now. Only have been able to find the string if it is known already.

    int count = 0;

  for(int i=str1.length-1;i>=0;i--)
  {
      String[] ElementOfArray = str1[i].split(" ");
      
      for(int j=0;j<ElementOfArray.length-1;j++)
      {
          if(ElementOfArray[j].equals("Hi"))
          {
              count++;
          }
      }
      
  }
  System.out.println(count);
  • 1
    How are you keeping track of the frequency for *N* timestamps? What limitations are there in terms of data structures? I'd consider parsing each String to a Map, and increment the counter. Partial example [is found here](https://stackoverflow.com/questions/44500446/count-occurrences-of-value-in-a-map) – KevinO Mar 25 '21 at 19:21

2 Answers2

0

One approach is to keep track of the number of entries, and increment.

    public static void main(String[] args)
    {
        String[] inp = {"1112323 400 error",
                "1112323 400 error",
                "9988778 400 error"};
                
        
        Map<String,Integer> results = new HashMap<>();
        
        for (String one : inp) {
            String[] parts = one.split(" ");
            
            String ts = parts[0];
            
            int val = results.computeIfAbsent(ts, v-> 0);
            results.put(ts, ++val);
        }
        
        System.out.println(results);
    }

Note: there are other ways to handle the map incrementing. This is just one example.

Sample Output:

{1112323=2, 9988778=1}

Now, if in the future one might want to perform other operations, using objects might be of interest.

So a class might be:

   private static class Entry
    {
        private final String ts;
        private final String code;
        private final String desc;
        
        public Entry(String ts, String code, String desc)
        {
           
            // NOTE: error handling is needed
            this.ts = ts;
            this.code = code;
            this.desc = desc;
        }
        
        
        public String getTs()
        {
            return ts;
        }
        
        
        public static Entry fromLine(String line)
        {
            Objects.requireNonNull(line, "Null line input");
            
            // NOTE: other checks would be good
            String[] parts = line.split(" ");
            
            // NOTE: should verify the basic parts
            return new Entry(parts[0], parts[1], parts[2]);
        }
        
        // other getter methods
    }

And then one could do something like:

        List<Entry> entries = new ArrayList<>();
        for (String one : inp) {
            entries.add(Entry.fromLine(one));
        }
        
        Map<String,Integer> res2 = entries.stream()
                .collect(Collectors.groupingBy(x->x.getTs(),
                                               Collectors.summingInt(x -> 1)));
        
        System.out.println(res2);

(same sample output at the moment). But if one needs to extend to count the number of 400 codes or whatever, it is trivial to change the stream since the object has the data. Of course, there are even more extensions to this approach.

KevinO
  • 4,303
  • 4
  • 27
  • 36
0

You can use HashMap to solve count the frequency of timestamps.

import java.util.HashMap;

public class test {
    public static void main(String[] args) {
        // Create a HashMap object called timeFrequency
        HashMap<String, Integer> timeFrequency = new HashMap<String, Integer>();

        String []str1 = {"1112323 400 error","1112323 400 error","9988778 400 error"};
        for(int i=0;i<str1.length;i++)
        {
            String[] ElementOfArray = str1[i].split(" ");
            if(timeFrequency.containsKey(ElementOfArray[0])){
                timeFrequency.put(ElementOfArray[0], timeFrequency.get(ElementOfArray[0]) + 1);
            }else{
                timeFrequency.put(ElementOfArray[0], 1);
            }

        }
        System.out.println(timeFrequency);
    }
}
Output:
{1112323=2, 9988778=1}
risingStark
  • 1,153
  • 10
  • 17