naik21
naik21

Reputation: 15

Find a particular word in array of sentences and return the sentences that contain the word

The input is String array as below,

Sample log line: 001 555 3334523 AppName 2341 This is a message. 001 1224 3334524443 AppSecond 2341 This is a message blah 341""-*. 022201 55555 3333334523 AppThird 2341 This is some other message. 0301 533555 3334523 AppName 2341 This is another message.

I need to print all the lines that contain AppName in it.

This is what I have tried.

  public static void findArrayString(String[] input, String item)
  {
    for(int i = 0; i < input.length; i++)
     {

      List<String> chr = new ArrayList<>();
        chr = Arrays.asList(input[i]);

      if(chr.get(i).contains(item))
      {
        System.out.println(input[i]);
      }

    }
  }

  public static void main(String[] args) 
  {
    String[] str = {"001 555 3334523 AppName 2341 This is a message.",
                   "001 1224 3334524443 AppSecond 2341 This is a message blah 341-*.",
                   "022201 55555 3333334523 AppThird 2341 This is some other message.",
                    "0301 533555 3334523 AppName 2341 This is another message."};

    findArrayString(str,"AppName");

  }
}

My output is :

001 555 3334523 AppName 2341 This is a message.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at java.base/java.util.Arrays$ArrayList.get(Arrays.java:4351)
    at Solution.findArrayString(Solution.java:31)
    at Solution.main(Solution.java:47)

Can anyone please tell me where I am going wrong?

Upvotes: 0

Views: 54

Answers (3)

naik21
naik21

Reputation: 15

Simple Answer

class Solution
{
  public static void main(String[] args)
  {
    String s = "001 555 3334523 AppName 2341\" This is a message. 001 1224 3334524443 AppSecond 2341 This is a message blah 341\"\"-*. 022201 55555 3333334523 AppThird 2341 This is some other message. 0301 533555 3334523 AppName 2341 This is another message.";
    
    String[] str = s.split("\\.");
    for(String st : str)
    {
      if(st.contains("AppName"))
      {
      System.out.println(st);
      }
    }
    
  }
  
}

Upvotes: 0

Vader
Vader

Reputation: 179

You are breaking the paragraph into sentences with one for loop but in order to break the sentences into words you need to use another for loop so that comparisons can be made on the words.Here is the correction needed in the code :

public static void findArrayString(String[] input, String item)
      {
        for(int i = 0; i < input.length; i++)
         {

          List<String> chr = new ArrayList<>();
            chr = Arrays.asList(input[i]);

            for(int j=0;j<chr.size();j++) {
                if(chr.get(j).contains(item))
              {
                System.out.println(input[i]);
              }
            }      
        }
      }

Upvotes: 0

Andrew S
Andrew S

Reputation: 2756

  List<String> chr = new ArrayList<>();
  chr = Arrays.asList(input[i]);

creates a List with a single element. But then

if(chr.get(i).contains(item))

attempts to find the ith element of chr. Since there is only a single element the exception is thrown when i > 0.

The intermediate list is not needed - try something like this within the loop:

  if(input[i].contains(item))
  {
    System.out.println(input[i]);
  }

Upvotes: 1

Related Questions