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: Fetch string in JAVA

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

    Default Fetch string in JAVA

    Hello,

    I would like to check a string contains "hello" or not?

    In the following string if we remove some characters we have "hello".

    ahhellllloou

    sdfhwesflrfgvloouhhlh

    But the following string is not matched with "hello".

    hlelo


    I wrote a program and it works fine but Do we have a simple way? for example with regex?

    Thanks


    package newpackage;
     
    import java.util.*; 
     
    public class NewMain 
    {
     
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
     
            String myString = input.next().toLowerCase();
            char[] myChar = new char[myString.length()]; 
            char[] finalChar = new char[5];
            int j = 0;
            for (int i = 0; i < myString.length(); i++) 
            {
                if ((myString.charAt(i) == 'h') ||
                        (myString.charAt(i) == 'e') ||
                        (myString.charAt(i) == 'l')
                        || (myString.charAt(i) == 'o'))
                    myChar[j++] = myString.charAt(i);
            }
            int countH =0, countE = 0, countL = 0, countO = 0;
            int FcountH =1, FcountE = 1, FcountL = 2, FcountO = 1;
     
            for (int i = 0; i < myChar.length; i++) 
            {
                if (myChar[i] == 'h')
                    countH++;
              else  if (myChar[i] == 'e')
                    countE++;
              else  if (myChar[i] == 'l')
                    countL++;
              else  if (myChar[i] == 'o')
                    countO++;
            }
            j = 0;
             for (int i = 0; i < myChar.length; i++) 
            {
                  if (myChar[i] == 'h')
                  {
                      if (countH > 1 && FcountH == 1){
                          finalChar[j++] = 'h';
                          countH--;
                          FcountH = 0;
                      }
                      else if (countH == 1 && FcountH == 1)
                      {
                           finalChar[j++] = 'h';
                           FcountH = 0; 
                      }
     
                  }
                  else if (myChar[i] == 'e' && FcountE == 1)
                  {
                      if (countE > 1){
                         finalChar[j++] = 'e';
                          countE--;
                          FcountE = 0;
                      }
                      else if (countE == 1 && FcountE == 1){
                            finalChar[j++] = 'e';
                              FcountE = 0;
                                      }
                  }
                 else if (myChar[i] == 'l' )
                  {
                      if (countL > 2 && FcountL >= 1){
                             finalChar[j++] = 'l';
                          countL--;
                          FcountL--;
                      }
                       else if (countL >=1 &&  FcountL != 0){
                            finalChar[j++] = 'l';
                             countL--;
                          FcountL--;
                       }
                  }
                 else if (myChar[i] == 'o' &&  FcountO == 1)
                  {
                      if (countO > 1){
                           finalChar[j++] = 'o';
                          countO--;
                          FcountO = 0;
                      }
                      else if (countO == 1 &&  FcountO == 1){
                            finalChar[j++] = 'o';
                             FcountO = 0;
                       }
                  }
            }
            String finalString = new String(finalChar);
            if (finalString.equals("hello"))
                        System.out.println("YES");
                        else
                        System.out.println("NO");
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Fetch string in JAVA

    There might be a way with regex. Give it a try.
    I don't do much with regex.

    What is the logic for doing the checking?
    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 (December 9th, 2020)

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

    Default Re: Fetch string in JAVA

    Quote Originally Posted by Norm View Post

    What is the logic for doing the checking?
    Thank you, sorry my English language is bad.

    I did not get you.

    I am learning JAVA language.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Fetch string in JAVA

    I do not use regex and I can't help with trying to use regex.

    The posted code looks too complicated.
    To simplify the code, can you start by listing the steps the code needs to take to solve the problem?
    Once there is a good list of steps the program needs to take (algorithm) you can try to write the code.
    What are the program's requirements?

    A suggestion: use a method to do the search. Pass the method two arguments: the word to search for and the String to be searched. Return true if found, else return false if not found.
    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 (December 9th, 2020)

Similar Threads

  1. java Api to fetch the CPU Details
    By ashish12169 in forum Java SE APIs
    Replies: 1
    Last Post: January 11th, 2024, 01:13 AM
  2. Fetch Data and Render
    By bheemar in forum JDBC & Databases
    Replies: 6
    Last Post: February 10th, 2014, 03:20 PM
  3. Replies: 1
    Last Post: September 10th, 2013, 06:03 AM
  4. how to fetch data from MySQL into textarea...
    By Ravi Raj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 7th, 2013, 05:18 AM
  5. How to fetch integer data from excel
    By nehakuls in forum JDBC & Databases
    Replies: 5
    Last Post: May 14th, 2010, 01:44 AM