SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jarred Olson wrote:Think about what would your code would produce with the following:
You're on the right track by comparing the chars at every point, but you need to make sure they are the same length before comparing the individual chars. Also take a look at what you're returning.
Fifi Akt wrote:
This is the part I am in trouble
What is the magic of while loop? I am really having hard time with while loops. how we assign the values? What are the criteria?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:often, the best way to start is to write down in English (or whatever your native language is) how you would do this. For example, if i had to count the number of times the letter 'e' appeared in a string,
1) count the number of 'e's in the string
then i'd look at each line and see if i know how to do that in java. if not, try refining it:
1) Start at the 1st character
2) see if it is an 'e'
3) if so, add one
4) go to the next letter
etc.
For comparing two strings, you'd do something like
1) get the length of each.
2) if they are not the same, then i can quit and return false
3) if they are, compare character by character
4) if at any point, the two chars are different, quit and return false
5) if i get to the end of the string(s), i can return true.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:I don't understand why you would post some code, then ask US if it works. Did you compile it? Did you run it? Did you test it? What happened?
YOU should be able to answer the question of "does this code work" yourself. If it does - GREAT!!! you're done. if not, then you need to figure out WHY. You need to figure out what you think it should be doing, and what it really is doing. If you can't reconcile those two things, THAT is what we can help you with - assuming you tell us.
Again, I'm not trying to be mean or abusive, but you really don't seem to be putting much effort into this. We aren't here to do the work FOR you. Please try and put in a decent effort.
As far as you code... I would say you didn't follow the steps I outlined at all. The first thing I outlined was "get the length of each. "
the first thing you did was declare a position holder variable, and enter a loop.
So... back up more. Write code for the first step. Write your method to do NOTHING but get the length of each string, then maybe print that out. I.e., if you pass in "fred" and "bob", it should do nothing more than print "4 3". Once you have that, change it/add code so that if they lengths are different, you print "Strings are not equal", and return false - otherwise return true.
Your code will not be done, but it will compile and run at every step. you add a piece at a time, test that, and only when you are SURE it works, do you add more. this process is called iterative development, and it really the best way to do things.
Fifi Akt wrote:I believe you are being judgmental. I am really trying hard to understand it. Have no basic knowledge have no general idea. You do not have to answer my questions if you think I do not put effort as much as you think I have to. Even it is not in my own language.
I am kind of person which looks for corrections step by step, but you have no right to criticize me.Sorry to disturb
Experience and talent are independent of age
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|