• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A small request

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I was doing one part of my homework, But I am not sure whether I am on the right track.Could you look at this?

Given two String parameters, the following method returns true if they are the same string of characters, and false otherwise. Note: You cannot use the equals String method; in other words, you actually have to write the code that compares the two strings character by character.

public static boolean stringsAreEqual(String s1, String s2)
{
// fill this in
}


 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First question: Does it even compile? Let's start there with the compiler complaints. You may wish to post them and your thoughts as to their solution.

Then we will need to discuss whether your method should have System.out.println statements within it. Is the goal of this method to output Strings as it's processing or to return information to the caller? What information is supposed to be returned? Does it do this?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if s2 is shorter than s1?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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?
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While loops will run between the curly braces until the expression inside the parenthesis evaluates to false. Some simple examples:



Try working with some simple code like the ones above and changing the values around to see how different values effect the output. I find the best way to learn how to use some code is to jump into it and play around with it and see what works and what doesn't.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fifi Akt wrote:
This is the part I am in trouble


You may not be able to use the String.equals(...) method, but you can use other String methods. Are there any methods of String that would allow you to get String lengths and compare them? The hint / key here is that you want to get the length of the String. Perhaps you should look for a String method that may have this term in it.


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?


First determine if you really need to use while loops. A while loop is generally used if you don't know in advance how many times you will need to loop. A for loop on the other hand is usually used when you do know in advance how many times you'll go through the loop. Since you'll be able to get the String length(), you'll know in advance how many times you'll need to loop. So I would not use a while loop here.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go Step wise, First the strings should be compared for meaningful equivalents only when the two strings are equal. Well if they aren't equal then just return false. If they are equal then check the character in each position. I think the check condition should be even if 1 character is not equal just return false.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fifi Akt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





public static boolean stringAreEqual(String s1, String s2)

{

int pos=0;

while (pos!=0)

{

if(s1.length()==s2.length()){

}

if( s1.charAt(pos)==s2.charAt(pos)){

System.out.println(“same”)

}

Else(“not same”)

{

System.out.print

pos++;



}

return str1;

}

Does it work? I kinda try to follow the steps? Thank you
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This... does nothing. As you set the pos variable to something that immediately fails the while loop.



So... if the length of the two strings are the same, you do nothing.



This... of course.... is wrong. Two strings are equal when all the characters are equal -- not just if one character are equal.

Henry
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No that code won't work either. The while loop will never be entered since you're doing the following:


A for loop would be much better for this situation, but if you want to stay with the while loop this should get you on the right track:
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



I'm very much in agreement with Fred here. I don't think he is being judgemental. I think he is trying to teach you what is necessary to be a developer.

If you need to be taught "step by step" then I think you may be in the wrong field, as that is a luxury that even seasoned developers don't get. Computers have a knack for doing exactly what they are told. And humans have a knack of assuming. After more than a couple of decades, I still get surprised at the things that can happen. You need to get into the mode of always trying stuff. You need to get into the mode of always examining stuff. You need to get into a mode of checking everything, and never assuming.

A developer is not about knowing a fix set of skills. If it were, Fred would just give you the answer, and you can move on.


[EDIT: in thinking about this a bit more... if you didn't take any of Fred's suggestions, because he was too blunt, then there is the same unlikely chance that you would take any of my suggestions as well. So... feel free to ignore.]

Henry
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fifi,
These people here are trying to help you. For you your aim might be to simply get the solution or also understand what's happening and you might have a different approach but at the ranch we aim at leaning by experience. That gives us reason.

It would be good if you stepwise answer to Fred's or Henry's ideas, like what happened when you did that, what you found out etc. This would keep us interested in helping you.

For you I think it would be a good idea to make a flowchart or pseudocode first. Then try to code it.

Regards
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe in iterative development too [just didnt know there was a word for it ]
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic