Write a method called endsWith that takes two Strings as arguments, and returns true if and only if the first String ends with the characters in the second String. I.e., endsWith("hibbert", "bert") is true, and endsWith("hibberty", "bert") is false. Do not use the endsWith method that exists
in the String class.
Boolean endsWith( String string1, String string2)
char lastLetter = string1.charAt(string1.length() -1 )
I am still having problem with implementing this method, please help
Started by lina, Dec 11 2010 05:45 AM
8 replies to this topic
#1
Posted 11 December 2010 - 05:45 AM
|
|
|
#2
Posted 11 December 2010 - 06:22 AM
Why di you open the same thread twice? Just ask for more help in the other one..
Anyway..
Next time please try to do you homeworks first, and THEN ask for help.
Anyway..
public boolean endsWith(String string1, String string2){
return string1.charAt(string1.length()-1)==string2.charAt(string2.length()-1);
}
Next time please try to do you homeworks first, and THEN ask for help.
#3
Posted 13 December 2010 - 09:37 AM
Confused as to why you are comparing the chars like that. I presume you are looping and the length of the search string somewhere else.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#4
Posted 13 December 2010 - 09:43 AM
No I'm not looping anywhere. The method has to return true if the two words end with the same char. Since you take the last char of a word by doing:
you just have to compare these two. The code can be rewritten like this if you understand it better:
word.charAt(word.length()-1)
you just have to compare these two. The code can be rewritten like this if you understand it better:
public boolean endsWith(String string1, String string2){
char lastChar1 = string1.charAt(string1.length()-1);
char lastChar2 = string2.charAt(string2.length()-1);
if(lastChar1==lastChar2)
return true;
else return false;
}
#5
Posted 13 December 2010 - 10:30 AM
I was not really questioning your methods but lina's. It seems as though a comparison between two chars are being made. But lina asked about comparing strings hence my reason for wondering if there was a loop to process the string.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#6
Posted 13 December 2010 - 10:33 AM
You don't have to compare strings, but just their last character.
#7
Posted 13 December 2010 - 10:38 AM
lina said:
Write a method called endsWith that takes two Strings as arguments, and returns true if and only if the first String ends with the characters in the second String. I.e., endsWith("hibbert", "bert") is true, and endsWith("hibberty", "bert") is false. Do not use the endsWith method that exists
in the String class.
in the String class.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#8
Posted 13 December 2010 - 10:43 AM
Ouch, i'm sorry, but I've read character instead of characters.
In this case, lina, you just have to iterate through the last string2.length() characters of the first string to check if they're equal to the corresponding characters of the second.
In this case, lina, you just have to iterate through the last string2.length() characters of the first string to check if they're equal to the corresponding characters of the second.
#9
Posted 13 December 2010 - 01:02 PM
eafkuor said:
Ouch, i'm sorry, but I've read character instead of characters.
In this case, lina, you just have to iterate through the last string2.length() characters of the first string to check if they're equal to the corresponding characters of the second.
In this case, lina, you just have to iterate through the last string2.length() characters of the first string to check if they're equal to the corresponding characters of the second.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









