Stephen Son

Greenhorn
+ Follow
since Dec 04, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Stephen Son

@Henry Wong As for clarification, I can understand it well. Maybe other people would have difficulty understanding it. But everyone's brain works differently. The only flaw is that the loop breaks and returns false as soon as a match is not found in Part2, like "shaky" and "shkshaky". This was brought to my attention on another forum. I also notice that the loop will end if the last character of string2 is a match to the first character of string1 in the case of "abc" and "bca". Because "bca" ends with "a", there are no more characters left to compare. Yes I can sort of envision it into code. The program is really complex so it's really hard to envision.

@Winston Gutkowski I see what you mean now. Sorry I should have worded the problem better like this:

Write a method that checks whether a the first string is a substring of the second string. Write a test program that prompts the user to enter two strings, and check whether the first string is a substring of the second. Do not use indexOf or contains methods.
_________________________________________________________________________________________________________

Here is the most up to date version of my code:

I told my professor I would send her the program today. I tried moving the breaks and adding another if statement. Some strings that didn't work before would work and other strings that did work before didn't work. I know it's not perfect but I am going to turn in my code like this. This topic is not resolved so I will not mark it as resolved. I'm not saying I'm giving up. I'm just saying it's time to turn it in. She is probably wondering why I haven't turned it in yet. This is the hardest java problem I had to do. No wonder it was on the very last homework assignment. Thank you to everyone who responded to this thread. I appreciate your help and in the process I have learned a lot. I probably won't be taking another java class in the future because this is really hard.

6 years ago
@Raymond Tong Oh I see. Thanks. "bc" is shorter than "abc". When string 2 is shorter than string 1, the method should return false. Ok I will add that to my code.

@Winston Gukowski What interpretation? If you didn't know, I actually wrote the problem. The problem in the book asks to use indexOf method. My teacher asked us not to use indexOf or contains method so I just reworded the problem from the book to meet her requirements. Yes, I'm missing a test to see if string2 is longer than string1. I will add that to my code.

@Henry Wong I'm not trying to avoid anything. I don't know why you think that. I rewrote the instructions and made them more clear.
0)check if string2 is longer than string1, if string2.length() > string1.length() proceed to step 1. If not, return false.
1)compare the first character of string1 to each of the characters of string2
2)If a match is never found, the method returns false
If a match is found eventually, count will record the position of the match. Break and proceed to Part2 of program
3)the second character of string1 will be compared to the character that succeeds the matching character of string2
4)If a match is not found, the method returns false
If a match is found, proceed to step 4, counter will record the position of the new match
and it will be added to count so that the loop will start at this position the next time.
5)the third character of string1 will be compared to the character that succeeds the next matching character of string2
6)Repeat until all characters of string1 have matched(return true) or until a match is not found(return false).

The messages tell me that the first character of string1 is being compared to each character of string2 and so on. Only the count/counter is missing so I added it to my code earlier. Yes I understand what the computer is doing. The loop should break after a match is found so I added that to my code earlier. What is the next step after that?
6 years ago
@Deepak Soni Yeah I noticed that too.

I made some changes to my code and it seems to be working correctly. All the strings I've tested seem to work. However I'm not 100% certain my code is correct. Is there anything wrong with it?

6 years ago
This topic has been cross posted here.

@Henry Wong Ok I see. Here are the steps:
1)compare the first character of string1 to each of the characters of string2
2)If a match is not found, the method returns false
If a match is found, proceed to step 3
3)the second character of string1 will be compared to the character that succeeds the matching character of string2
4)If a match is not found, the method returns false
If a match is found, proceed to step 4
5)the third character of string1 will be compared to the character that succeeds the next matching character of string2
6)Repeat

@Raymond Tong Thanks, debugging printout wasn't covered in my java class. I will add them for now and delete them before I turn it in.

d and c have been switched in line 26. And debugging printout has been added.


This is the output:

Please enter a String: ant
Please enter a second String: bath
MATCH: N The value of c and d: a and b
MATCH: Y The value of c and d: a and a
MATCH: N The value of c and d: a and t
MATCH: N The value of c and d: a and h
MATCH: N The value of c and d: n and b
MATCH: N The value of c and d: n and a
MATCH: N The value of c and d: n and t
MATCH: N The value of c and d: n and h
MATCH: N The value of c and d: t and b
MATCH: N The value of c and d: t and a
MATCH: Y The value of c and d: t and t
MATCH: N The value of c and d: t and h
The first string is a substring of the second.


How do I do the next step?
6 years ago
No, the output is not correct. For both examples, the first is not a substring of the second.
As for the first step, I have no idea what I'm doing wrong. That's why I asked "What would I change to get the code to do work correctly?".
6 years ago

Raymond Tong wrote:May be you could break down your logic to steps.
And put comment next to your codes about the "step" it implemented.

And as suggested above, add debugging printout. (Insert System.out.println without removing existing code)


Even if I break down my logic into steps, I still don't know how to write code for each step. I read your explanation on debugging printout but I'm still not sure what it is. Can you show me an example?
6 years ago
I tested both examples and both times the program says "The first string is a substring of the second."

Paul Clapham wrote:

Stephen Son wrote:My logic is I am going to compare the first character of string1 to each of the characters of string2. If a match is not found, the method returns false.



Your code doesn't do that. In this algorithm you treat the first character of string1 specially -- as you should -- but in your code you don't. It does the same thing for all characters of string1.


I thought it did. I thought these 2 lines did that.

What would I change to get the code to do work correctly?

Paul Clapham wrote:

If a match is found, then the second character of string1 will be compared to the character that succeeds the matching character of string2. If those don't match, the method returns false. If they do match, the program will continue and so on.



Your code doesn't do that. There isn't anything there which looks for the character after the one you just looked at. It just goes through all characters in string2 every time.


I didn't realize that. I don't know how to do that. Would I add a third loop after "} else {" like this:

If not, then how do I get the method to look for the character it just looked at?
6 years ago
Ok I made some changes to my if else statment. Here's my code now:

I tested "ant" and "tyrant", "ant" and "alaska", "texas" and "vermont", "abc" and "bca" and got the right output. I think my code is working properly. I can't think of any examples where the program won't work. If you seen anything else wrong with my code, let me know.
6 years ago
Ok I fixed up my logic. My logic is I am going to compare the first character of string1 to each of the characters of string2. If a match is not found, the method returns false. If a match is found, then the second character of string1 will be compared to the character that succeeds the matching character of string2. If those don't match, the method returns false. If they do match, the program will continue and so on... Is it correct now?
6 years ago
You mean like this:

My logic is I am going to compare the first character of string1 to each of the characters of string2. If a match is not found, the method returns false. If a match is found, then I will compare the second character of string1 to each of the characters of string2 and so on... Is my logic correct?
6 years ago


@Keith Rainey I think that's right. I fixed up my i's and j's and it doesn't break.

@Henry Wong I tried those examples and the program reported that it matches. However, after fixing the i's and j's in my second loop, both examples do not report a match. With "ant" and "tyrant", it still does not work. What do you mean by debugging printouts? I've never heard of those.
6 years ago
Write a method that checks whether a string is a substring of another string. Write a test program that prompts the user to enter two strings, and check whether the first string is a substring of the second. Do not use indexOf or contains methods.



Here are some sample outputs from my program:

Please enter a String: ant
Please enter a second String: antarctica
The first string is a substring of the second.



Please enter a String: ant
Please enter a second String: tyrant
The first string is NOT a substring of the second.



The first output is correct. However the second output is wrong. "ant" is a substring of "tyrant". What is wrong with my code?

6 years ago

lokesh chenta wrote:Hi,
The check on count of digits(i.e, count<2 ) should happen once you have scanned through all the characters of your password i.e, after the for loop.
You can actually step through each line for yourself and see why it's not working.


Oh I see. Thanks. I fixed that in my code.
6 years ago
This topic has been cross posted here.

Since I haven't been getting any replies on that topic, I figured I would post this same question on this site.

Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rule is as follows:

  • A password must have at least 8 characters
  • A password consists of only letters and digits
  • A password must contain at least 2 digits


  • Write a program that prompts the user to enter a password and displays "Valid Password" if the rule is followed or "Invalid Password" otherwise.

    Here is my most up to date code:



    My code compiles and runs. The only part that isn't working is the part that checks to see if the password has at least 2 digits. How would I correct this?
    6 years ago