Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Check String Pattern in Java

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Check String Pattern in Java

    Hello,

    First I get a number (numbers of string to check) then get the pattern.
    If the string contains the pattern, print Yes otherwise No.

    For example:
    4 quera102

    quEra0012
    qu0erraa12
    sN0Ap12
    qurra00L

    No
    Yes
    No
    No


    I wrote the below code but I get 71 from 100.


    package My;
    import java.util.*;
     
    public class NewMain
    {
     
        public static void main(String[] args) 
        {
            Scanner scanner = new Scanner(System.in);
     
            int n = scanner.nextInt();
     
            String pattern = scanner.next();
     
            String []myString = new String[n];
     
            for (int i = 0; i< n ; i++)
            {
                myString[i] = scanner.next();
            }
            boolean flag = true;
               for (int i = 0; i< n ; i++)
            {
                flag = true;
                for (int j = 0 ; j < myString[i].length(); j++)
                {
                    int indexOf = pattern.indexOf(myString[i].charAt(j));
                    if (indexOf == -1)
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                    {
                       System.out.println("No");
                    }
                        else
                    {
               System.out.println("Yes");
     
                    }
            }
     
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Check String Pattern in Java

    I get 71 from 100.
    Does that mean the code misses 29 items?
    Can you print the 29 items that the code misses that should have been detected so we can see where it has the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (April 7th, 2021)

  4. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Check String Pattern in Java

    Quote Originally Posted by Norm View Post
    Does that mean the code misses 29 items?
    Can you print the 29 items that the code misses that should have been detected so we can see where it has the problem?
    My point is 71% from 100%

    We don't have failed examples.

    I attached the output and two conditions in question.

    https://i.ibb.co/9gmvHv8/Screenshot-...-07-155804.jpg

    https://i.ibb.co/WPMB4mw/Screenshot-...-07-155517.jpg

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Check String Pattern in Java

    Sorry, I can not copy anything from an image. Also I do not go to other sites.
    Please copy the items that were not correctly identified and paste them here.
    Without examples of input that was incorrectly identified, I don't know how to work on the problem.

    two conditions in question.
    Any conditions that the code should be testing for needs to be included in the source file as comments so that anyone reading the code knows what the code is trying to do.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (April 7th, 2021)

Similar Threads

  1. Replies: 3
    Last Post: October 11th, 2017, 05:47 AM
  2. [SOLVED] Pattern Matching to string problme
    By Kakashi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2011, 09:45 AM
  3. How to define a string pattern using variables?
    By ice in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2011, 10:45 PM
  4. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM
  5. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM