File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A small request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A small request" Watch "A small request" New topic
Author

A small request

Fifi Akt
Greenhorn

Joined: Jul 09, 2009
Posts: 10
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
}


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

What happens if s2 is shorter than s1?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jarred Olson
Ranch Hand

Joined: Jul 31, 2009
Posts: 37
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

Joined: Jul 09, 2009
Posts: 10
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

Joined: Jul 31, 2009
Posts: 37
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

Joined: Feb 23, 2007
Posts: 1561
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.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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
Fifi Akt
Greenhorn

Joined: Jul 09, 2009
Posts: 10
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jarred Olson
Ranch Hand

Joined: Jul 31, 2009
Posts: 37
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
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Jul 09, 2009
Posts: 10
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
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


Experience and talent are independent of age
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

I believe in iterative development too [just didnt know there was a word for it ]


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A small request
 
Similar Threads
Comparator..
Find one char in a String ? ? ?
Recursive anagram problem
palindrome program
comparator problem