2

I am trying to find a subString inside a string, in an arraylist, but I can't get it working. It has to loop through the arraylist until it finds the sub string, and then stop.

Here is what I have so far

    private static void teamSearch(String teamName) {

    String subString = teamName;
    String string = "";
    boolean contains = false;

    while (contains = false){
    for (int i = 0; i < clubList.size(); i++){
        string = clubList.get(i).aliases;
        contains = string.contains(subString);
    }
    System.out.println(contains);
    }
}

Any help in getting this working would be much appreciated :) Thank you!

1
  • Don't use while, break the for loop when constains is true. Commented May 30, 2013 at 7:03

7 Answers 7

1

use

while (contains == false)

instead

while (contains = false)

= is an assign operator and == is comparision operator. you are assigning false to contains in while loop. You have to compare the values

Sign up to request clarification or add additional context in comments.

Comments

1

Avoid while loop. Use break statement. If not careful, while loops can cause infinite loops.

private static void teamSearch(String teamName) {
    String subString = teamName;
    String string = "";
    boolean contains = false;

    for (int i = 0; i < clubList.size(); i++){
        string = clubList.get(i).aliases;
        if (string.contains(subString)) {
           contains = true; 
           break;
        }
    }
    System.out.println(contains);
}

Comments

0

Your inner for loop keeps on rolling - even if it finds the the alias with the substring.

You need to terminate the loop when it finds a match. This is a naive, yet correct implementation

private static void teamSearch(String teamName) {

    String subString = teamName;
    String string = "";
    boolean contains = false;

    for (int i = 0; i < clubList.size(); i++){
        string = clubList.get(i).aliases;
        if (string.contains(subString){ 
          contains = true;
          break;
        } 
    }
    System.out.println(contains);
}

Comments

0

Your while loop condition is wrong change it to while ( contains == false )

Comments

0

you are trying to assign a value there.you need to check the value equal or not

while (contains == false) //checking equality


while (contains = false) //assigning

Comments

0

You could use

while (!contains) {...}

But it might be easier to simply drop the while loop and break the for loop

for (int i = 0; i < clubList.size(); i++){
    string = clubList.get(i).aliases;
    contains = string.contains(subString);
    if (contains) {
        break;
    }
}

Comments

0

it could be useful

boolean flag = false;

    String subString = "nice";


    ArrayList<String> arrayList = new ArrayList<String>(2);


    arrayList.add("good morning");


    arrayList.add("that is nice");


    for (String element : arrayList) {


        if (element.contains(subString))


            flag = true;


        else


            flag = false;


    }

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.