Jump to content

I am still having problem with implementing this method, please help

- - - - -

  • Please log in to reply
8 replies to this topic

#1
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
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 )

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Why di you open the same thread twice? Just ask for more help in the other one..

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
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
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
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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:

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
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
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
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
You don't have to compare strings, but just their last character.

#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

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.
I think i may need glasses.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#8
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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.

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

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.
heh, i actually had the same problem.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users