Need help with Naive Approach for searching in JAVA

Problem Statement

Given a string S and a pattern P both of lowercase characters. The task is to check if the given pattern P exists in the given string S or not.

Input

First line of input contains number of testcases T. For each testcase, first line will the string and second line will be the pattern to be searched.

Constraints:
1 <= T <= 100
1 <= |S|, |P| <= 1000

Output

For each testcase, print “Yes” if pattern exists or “No” if doesn’t.

Code for the question :

public class Naive {

public static void search(String str, String pattern) {
            int n = str.length(); 
            int m = pattern.length(); 
    
    for (int s = 0; s <= n - m; s++) { 
         
        for (int j = 0; j < m; j++)
            if (str.charAt(s+j) != pattern.charAt(j) )
                System.out.println("No");
                break;
              if (j == m)
                System.out.println(" Yes");
                     } 
} 

public static void main(String[] args) 
{  Scanner sc = new Scanner(System.in);

    String txt = sc.nextLine();
    String pat = sc.nextLine(); 
   
    search(txt, pat); 
}

}

I will traverse the entire string to check if there are any matching patterns of my string . if there are any matches we will print at what index they are present if no match then we will break it.

it gives the index number when i try to run it but i want the output as Yes or No for pattern matching and it is not working for That.

If you run this you will get expected output

/*package whatever //do not write package name here /
import java.util.
;

public class Naive {

public static void search(String str, String pattern) {
            int n = str.length(); 
            int m = pattern.length(); 
    
    for (int s = 0; s <= n - m; s++) { 
         int j;
        for ( j = 0; j < m; j++)
        
            if (str.charAt(s+j) != pattern.charAt(j) )
            break;
              if (j == m)
                System.out.println(s);
                     } 
} 

public static void main(String[] args) 
{  Scanner sc = new Scanner(System.in);

    String txt = sc.nextLine();
    String pat = sc.nextLine(); 
   
    search(txt, pat); 
}

}

https://my.newtonschool.co/playground/code/jdvnc05izw4w/

yes I know I try code first on other ide before running on newton portal and I tried to edit it on newton portal to get expected output

https://my.newtonschool.co/playground/code/jdvnc05izw4w/

hi there,

your code isn’t compiling because your for loop is missing the declaration for j
Main.java:19: error: cannot find symbol if (j=m){ ^ symbol: variable j location: class Main 1 error

Edit: as Randell mentioned (and I asked you before in a previous thread), when posting on the forum, your code should be indented correctly.
It still isn’t.
Let us know if you don’t know what that means.

Also consider adding comments. It will help people read your code and help you become a better developer.

Can you tell me how to indent code properly I am beginner so you can teach me.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.