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: Substring program, not working

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Substring program, not working

    Hi

    Write a static method which takes two strings and returns a boolean saying whether the
    first string occurs as a substring within the second. For example, if the strings are
    “blis” and “antidisestablishment”, the method should return true. See if
    you can write two versions of the method, one using iteration, the other using
    recursion. Remember when you are asked to write “a method” it is perfectly
    acceptable to write extra helper methods which that method calls. In this case, do not
    make use of any of the built-in methods provided by Java in class String, apart from
    length, substring and charAt.

     
    import java.util.Scanner;
     
    class Lab3Q2
    // Lab3 Question2
    {
     
     public static void main(String[] args)
     {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter a string: ");
      String str1 = input.nextLine();
      Scanner input1 = new Scanner(System.in);
      System.out.println("Enter another string: ");
      String str2 = input1.nextLine();
     
     
      if (q2(str1,str2)) {
         System.out.println(str1 + " is , a substring of " + str2);
      } else {
         System.out.println(str1 + " is not, a substring of " + str2);
      }
     
     }
     
     public static boolean q2(String str1, String str2)
     {
      boolean word = false;
      for(int i =0; i < str1.length(); i++) {
          for(int j =0; j < str2.length(); i++) {
              if (str2.substring(j,str1.length())== str1){
                  word = true;
              } else {
                  word = false;
              }
          }
      }
      return word;
      }
     
     
    }

    my program doesnt seem to work, and I dont know why?
    Last edited by helloworld922; October 18th, 2009 at 09:46 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Substring program, not working

    You can't compare strings using the == operator, you need to use the .equals() method. Also, in your second loop don't start from 0, but start from i, and it should also be j++ instead of i++ in the second loop. Your substring is taking the wrong sub-string. Change it to take a substring from i to j+1. And lastly, you shouldn't return false right away, but that simply means that that starting character location isn't the correct place to be looking. Once you've looked through the entire str1 of starting locations and haven't found a match, then you can return false.

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

    Newoor (October 18th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Substring program, not working

    I didn't spot the i++/j++ thing. So thanks, in the end I used

    str2.substring(j,j+str1.length()).equals(str1)

    that worked fine, and just did a couple of return statements

    My Program is working. Thanks

  5. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Substring program, not working

    and dont need first loop

Similar Threads

  1. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM
  2. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM
  3. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 07:12 AM
  4. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 07:56 AM