coder123
coder123

Reputation: 37

Print statements inside for loop causing several outputs instead of one, how can I fix this without using "return"?

I am a beginner coder. My code needs to display "yes" if the string input consists of 'l' + 'any character' + 'l'. For example, "uwbıclsl" should have a "yes" output because a letter is sandwiched between two l's. However, since my print statements are inside of my for loop, it displays several "yes" and "no" s. How can I fix it so that I only have 1 output(yes or no) and still have print statements instead of returns?

for (int i = 0; i < s.length(); i++) {

            if ((s.charAt(i) == 'l') && (s.charAt(i + 2) == 'l')) {

                System.out.print("YES");

            } else
                System.out.print("NO");

Upvotes: 0

Views: 64

Answers (5)

Sash Sinha
Sash Sinha

Reputation: 22350

How about slightly modifying your current code to be in a separate method and subsequently returning "Yes" if you have found two l's separated by another char, since you do not need to carry on checking the rest of the string:

class Main {
  public static void main(String[] args) {
    String str = "uwbıclsl";
    System.out.println(charBetweenTwoLs(str));
    String str2 = "abclol";
    System.out.println(charBetweenTwoLs(str2));
  }

  static String charBetweenTwoLs(String str) {
    for (int i = 0; i < str.length() - 2; i++) { // Note the - 2 since you are checking on each iteration 2 characters ahead.
      if (str.charAt(i) == 'l' && str.charAt(i+2) == 'l') {
        return "Yes";
      }
    }
    return "No"; // return No if two 'l' characters were not found sperated by another character.
  }
}

Output:

Yes
Yes

Upvotes: 0

Derrick
Derrick

Reputation: 4407

Do the below changes -

String result = "NO";
for (int i = 0; i < s.length(); i++) {

            if ((s.charAt(i) == 'l') && (s.charAt(i + 2) == 'l')) {

                result = "YES";

            }
}

System.out.println(result);

Note - You dont need else block now, also you need to handle StringIndexOutOfBoundsException exception in your if statement, hope you can resolve this.

Upvotes: 0

Ludov Dmitrii
Ludov Dmitrii

Reputation: 572

You also can drop the cycle and use regex

String s = "uwbıclsl".matches(".*(l.l).*") ? "yes" : "no";
        System.out.println("s = " + s);

Upvotes: 0

Ali Ben Zarrouk
Ali Ben Zarrouk

Reputation: 2020

Better use regexes for this

System.out.println(s.matches(".*l.l.*") ? "YES" : "NO");

Upvotes: 1

javapedia.net
javapedia.net

Reputation: 2731

If you need to print Yes/No just once, try the below. Even if multiple matches, it just says Yes once. Also fixed "for" loop end length to (length()-2) to avoid Exception. Comment if you have any questions or this is not an expected solution.

boolean isFound = false;
for (int i = 0; i < s.length()-2; i++) {

            if ((s.charAt(i) == 'l') && (s.charAt(i + 2) == 'l')) {

              isFound = true;
            } 
}

System.out.println (isFound?"YES":"NO");

Upvotes: 0

Related Questions