Jump to content

Help with a program

- - - - -

  • Please log in to reply
4 replies to this topic

#1
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
I'm supposed to compare the end of two strings and see if they are equal. Here's my code:

String a = "s";

String b = "Hellos";

System.out.println(a.toLowerCase().substring(a.length() - 1, a.length()));

System.out.println(b.toLowerCase().substring(b.length() - 1, b.length()));

if (a.toLowerCase().substring(a.length() - 1, a.length()) == b.toLowerCase().substring(b.length() - 1, b.length()))

   System.out.println("true");

else

   System.out.println("false");

I put the two System.out.println at the top to see what comes out. Here, they look like they're equal, but it comes out as "false." What's going on here? It doesn't matter if they're uppercase or lowercase, so I put .toLowerCase on them.

#2
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
They are false because you use ' == ' instead of ' .equals(..)'
' == ' will check if their addresses in the memory are equal. equals() checks their value.

Know that strings have an endsWith() method..

#3
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts

wim DC said:

They are false because you use ' == ' instead of ' .equals(..)'
' == ' will check if their addresses in the memory are equal. equals() checks their value.

Know that strings have an endsWith() method..
Ahh yes, I totally forgot about .equals(). Anyway, this is a problem from CodingBat website, and I finally got it right, although it's quite lengthy.
public boolean endOther(String a, String b) {
  if (a.length() < b.length() && b.toLowerCase().substring(b.length() - a.length(), b.length()).equals(a.toLowerCase().substring(0, a.length())))
     return true;
  else if (b.length() < a.length() && a.toLowerCase().substring(a.length() - b.length(), a.length()).equals(b.toLowerCase().substring(0, b.length())))
     return true;
  else if (a.length() == b.length() && a.toLowerCase().substring(0, a.length() - 1).equals(b.toLowerCase().substring(0, b.length() - 1)))
     return true;
  else
     return false;
}
If any of you have a simpler solution, please let me know. Oh, and here's the link to the problem:
CodingBat Java String-2 endOther

#4
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
What I would do:

public boolean endOther(String a, String b) {

        a = a.toLowerCase();

        b = b.toLowerCase();

        return b.endsWith(a) || a.endsWith(b);

}


You can even put the toLowerCase on the return-line, but I don't like very long lines a lot.

#5
PhillipKessels

PhillipKessels

    Newbie

  • Members
  • Pip
  • 8 posts
I know it's cheatin'ยด, but in a lot of these exercises you could simply use the StringBuilder (have a look here). Internally it does the same as expected from you (string modification via "String"'s own methods).

//Edit: its actually not done by strings own methods, but with the help of an array of chars

Edited by PhillipKessels, 31 March 2011 - 08:02 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users