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 7 of 7

Thread: Comparing Strings only using the .length() method - possible?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Comparing Strings only using the .length() method - possible?

    Hey guys,

    I have been learning about Java for the past month or so, and have been using Arnow, Dexter and Weiss's Introduction to Programming Using Java. In Chapter 6, there's this Exercise 21, which basically states:

    "Write a method mySubStr() that receives two Strings and returns a boolean value: true only if one of the Strings is a substring of the other. Do not use any methods of the String class other than length()."

    Is this even possible? I mean, if you can't even use substring or indexOf or any others, how can you compare two Strings to see if one is a substring of the other?

    Thanks in advance for any help, as this question has really been bothering me. I'm just not sure if I don't know how to do it or there is a typo in the book.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Comparing Strings only using the .length() method - possible?

    It is probably cheating, but I would create a StringBuffer Object using the constructor StringBuffer(String str) and then use StringBuffer's substring that returns a String.

    I didn't use any of the other methods from the String class. If that is the only restriction, no one can say shit.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Ryker (October 16th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Comparing Strings only using the .length() method - possible?

    Quote Originally Posted by Ryker View Post
    Hey guys,

    I have been learning about Java for the past month or so, and have been using Arnow, Dexter and Weiss's Introduction to Programming Using Java. In Chapter 6, there's this Exercise 21, which basically states:

    "Write a method mySubStr() that receives two Strings and returns a boolean value: true only if one of the Strings is a substring of the other. Do not use any methods of the String class other than length()."

    Is this even possible? I mean, if you can't even use substring or indexOf or any others, how can you compare two Strings to see if one is a substring of the other?

    Thanks in advance for any help, as this question has really been bothering me. I'm just not sure if I don't know how to do it or there is a typo in the book.
    String (Java Platform SE 6)

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

    Default Re: Comparing Strings only using the .length() method - possible?

    Is that the exact word for word text of the book? What other assumptions can you make about your potential inputs?

    If this is indeed the exact wording of the problem and you're not allowed to use any other assumptions, it's not possible to determine if one string is a substring of another string using only the length() method.

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

    Ryker (October 16th, 2010)

  7. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Strings only using the .length() method - possible?

    Quote Originally Posted by javapenguin View Post
    Unfortunately, substring() does not seem to be allowed.

    Quote Originally Posted by aussiemcgr View Post
    It is probably cheating, but I would create a StringBuffer Object using the constructor StringBuffer(String str) and then use StringBuffer's substring that returns a String.

    I didn't use any of the other methods from the String class. If that is the only restriction, no one can say shit.
    Ah, OK, I didn't know about the StringBuffer object, but since these haven't been introduced in the book yet, I don't think the authors had that in mind. The exercises at the end of each chapter namely pertain to the stuff that was covered in the chapter, and although they are challenging, they usually involve only the stuff that was covered and don't require readers to probe Java documentation for suiting methods.

    Quote Originally Posted by helloworld922 View Post
    Is that the exact word for word text of the book? What other assumptions can you make about your potential inputs?

    If this is indeed the exact wording of the problem and you're not allowed to use any other assumptions, it's not possible to determine if one string is a substring of another string using only the length() method.
    Yeah, that is the exact wording, and I arrived to the same conclusion, but just wanted to check with people who are more versed in Java. This would then not be the first error in the book, as there are some typos, such as when they introduced assigning a variable to System.out and forgot to mention you need to import java.io.* for this to work. So yeah, there's been times I've been really frustrated with something not working only to find out later that something's missing in the explanation.

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

    Default Re: Comparing Strings only using the .length() method - possible?

    Are you taking a class in college? You should ask your professor about this. I've rarely had to re-define the standard system streams (System.in, System.out, System.err), you don't need to import java.io if you're just using these streams. As aussiemcr said, on a technicality you could use the StringBuilder constructor since it's technically not in the String class

    I like using Eclipse because it will automatically include imports for me, or if it can't figure out which import to use, it will ask me where I want to import it from. Saves me having to memorize which packages different classes are found in

  9. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Strings only using the .length() method - possible?

    Quote Originally Posted by helloworld922 View Post
    Are you taking a class in college? You should ask your professor about this. I've rarely had to re-define the standard system streams (System.in, System.out, System.err), you don't need to import java.io if you're just using these streams. As aussiemcr said, on a technicality you could use the StringBuilder constructor since it's technically not in the String class
    No no, I didn't mean redefine them, just, assign a variable to them, so that instead of writing, say, System.out.print..., I would just write [chosen variable name].print... But yeah, I am taking a class at the university, so I may just ask the professor about this, as well.

    Quote Originally Posted by helloworld922 View Post
    I like using Eclipse because it will automatically include imports for me, or if it can't figure out which import to use, it will ask me where I want to import it from. Saves me having to memorize which packages different classes are found in
    Yeah, I use Eclipse, as well, but when I started out and stumbled upon the above-mentioned problem, I didn't know it tells you what's missing.

Similar Threads

  1. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  2. [SOLVED] Method of Sorting? by assiging numerical values to Strings
    By Mirage in forum Java Theory & Questions
    Replies: 0
    Last Post: June 16th, 2010, 07:49 PM
  3. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 07:24 AM
  4. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM