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.
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.
Re: Comparing Strings only using the .length() method - possible?
Quote:
Originally Posted by
Ryker
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)
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.
Re: Comparing Strings only using the .length() method - possible?
Quote:
Originally Posted by
javapenguin
Unfortunately, substring() does not seem to be allowed.
Quote:
Originally Posted by
aussiemcgr
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
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.
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 :P
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 :)
Re: Comparing Strings only using the .length() method - possible?
Quote:
Originally Posted by
helloworld922
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 :P
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
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.