1

I can't figure out why this does not work? I am trying to locate keywords from an array in a string and to println the index number of the array in the console. I have tried the "if" statement both with and without the boolean "true"

public class Testing 
{
    public static void main(String[] args)
    {
    String[] keywords = new String[5];
    keywords[0] = "boat";
    keywords[1] = "car";
    String myString = "the banana car";

          for(int a = 0; a <= keywords.length; ++a)

          {

             if(myString.contains(keywords[a])== true)
                {

                System.out.println(myString.indexOf(keywords[a]));
                }
             else
                {
                System.out.println("Those keywords are not in that string"); 
                }

           }
     }
}
0

6 Answers 6

4
String[] keywords = new String[5];////////////here you have array with 5 element
    keywords[0] = "boat";
    keywords[1] = "car";
//////////you just full index 0 and index 1

then you for loop on the length of keywords

for (int a = 0; a <keywords.length; a++) 
//////keywords.length=5 , index 0 and index 1 have a value but the other index is empty 

So :

  • you need to full the keywords array :

     keywords[0] = "boat";
     keywords[1] = "car";
     keywords[2] = //////////;
     keywords[3] = //////////;
     keywords[4] = ///////;
    
  • or make the length =2:

    String[] keywords = new String[2];
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - I just tried changing String[] keywords = new String[2]; and it still doesn't work :(
try to change the for loop to :for (int a = 0; a <keywords.length; a++) don't use a <=keywords.length
1

you are missing 3 extra elements as you have declared your String array to contain 5 elements

String[] keywords = new String[5];
    keywords[0] = "boat";
    keywords[1] = "car";

Comments

1

a <= keywords.length should be a < keywords.length and you should fill the array if you declared it to be of size 5 to have 5 elements (from 0 to 4).

Arrays are zero based in Java, so if you have array of length n then the indexes are from 0 to n-1 (which will sum up to n)

Another important thing:

In if(myString.contains(keywords[a])== true), the == true is redundant, since myString.contains(keywords[a]) returns true or false and here you only want to check this. So it's a better style to remove the == true.

Comments

1

You haven't initalized all the elements of your keywords, therefore you are going to get a NullPointerException

If you only want to check the first two elements that you initialized, change your for loop to the following:

for (int a = 0; a <= 1; ++a)

Comments

1

This program:

public class Testing {

   public static void main( String[] args ) {
      String[] keywords = new String[]{ "boat", "car" }; // Only 2 not 5
      String myString = "the banana car";
      for( String keyword : keywords ) {
         int index = myString.indexOf( keyword );
         if( index > -1 ) {
            System.out.println(
               "Keyword '" + keyword + "' is in the string '" +
               myString + "' at position " + index ); 
         }
         else
         {
            System.out.println( "Keyword '" + keyword +
               "' is not in the string '" + myString + "'" ); 
         }
      }
   }
}

outputs:

Keyword 'boat' is not in the string 'the banana car'
Keyword 'car' is in the string 'the banana car' at position 11

2 Comments

+1 for using indexOf for the search and saving the redundant contains. also for the altered output in the else block.
Novice java guy says a big "Thank You" to all of you :)
0
public static void main(String[] args) {
    String[] keywords = {"boat", "car"};
    String myString = "the banana car";
    for(int i = 0; i < keywords.length; i++) {
        System.out.println(myString.contains(keywords[i]) ? myString.indexOf(keywords[i]) : "Those keywords are not in that string");

    }
}

..should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.