-2

I'm trying to show a name and a region where the last letter of the name equals the last letter of the region. Below is my attempt that I cannot get quite right:

static void oneLine(String name, String region,
               int area, long pop, long gdp)
{
  if (name.substring(name.length() - 1)  == name.substring(region.length()-1))
    {
      System.out.println(name+"  "+region);
    }
 }
Joseph Idziorek
  • 4,395
  • 6
  • 22
  • 35

4 Answers4

2

Use equals(Object anObject) method rather using == operator

if (name.substring(name.length() - 1).equals(region.substring(region.length()-1))){
  System.out.println(name+"  "+region);
 }

See Why would you use String.Equals over == for more details.

Community
  • 1
  • 1
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
  • equels is not `java`. Also after equals you take `name.substring` again instead of `region.substring` – Jordi Castilla Sep 29 '15 at 11:39
  • can't see it, still equels.... about name was not a typo, was your mistake trying to be fast answering. BTW... You have a nice chance to get disciplined badge not mantaining this answer... you know it's bad.... – Jordi Castilla Sep 29 '15 at 11:44
  • @JordiCastilla Yes of course it was mistake, thats why i says Typo mistake. Nevertheless thanks for point out. – Sumit Singh Sep 29 '15 at 11:53
  • sorry but no.... a [**typo**](https://en.wikipedia.org/wiki/Typographical_error) is when you missclick a letter in the keyboard, for example, the `equels`, what you corrected in first moment was `name.substring` that was a **mistake** nothing related to a [**typo**](https://en.wikipedia.org/wiki/Typographical_error) – Jordi Castilla Sep 29 '15 at 11:55
  • Yes you are right, but i was talking about **equals** spelling that was **equels** – Sumit Singh Sep 29 '15 at 12:00
  • I was talking about your comment *@JordiCastilla thanks, typo corrected. – Sumit Singh 19 mins ago* where **equels** was **NOT** corrected, again, no problem, but admit your mistake because you wanted to be the first – Jordi Castilla Sep 29 '15 at 12:01
  • As I told you, you have a nice change to get disciplined badge, and also to remove your downvote to my answer, mine it's already accepted, sorry – Jordi Castilla Sep 29 '15 at 12:01
2

Couple of things:

  • You are getting name.substring in both sides of equality

       if (name.substring(name.length() - 1)  == name.substring(region.length()-1))
       //                                        ↑ name again!!! 
    
  • NEVER compare strings with == in Java

if (name.substring(name.length() - 1).equals(region.substring(region.length()-1)))
Jordi Castilla
  • 25,851
  • 7
  • 65
  • 105
0

First, You are using name in both sides of the condition. Rather use:

if (name.endsWith(region.substring(region.length()-1))) { ... }

Second, if you are using == with String, you are using identity. This means that this will only result in true if you are using the same phisical String object. So:

new String("Something") == new String("Something") // Evaluates to false

The way to get around this is to use equivolance. This is done by using the equals method. When impelemnted this method has to follow the following rules:

  1. reflexive - If A is compared to A, return true
  2. symmetric - If A is equal to B, then B must be equal to A
  3. transitive - If A is equal to B and B is equal to C, then A must be equal to C
  4. If A is compared to null, always return false.

The String class implements this is such a way that it will check if the characters are the same. So:

new String("Something").equals(new String("Something")) // Evaluates to true

// BUT

new String("Something").equals(new String("something")) // Evaluates to false - due to case sensitivity. 

// SO

new String("Something").equalsIgnoreCase(new String("something")) // Evaluates to true

Hope this helps.

Nicholas Robinson
  • 1,379
  • 1
  • 9
  • 20
-2

I would propose not to create one character long substrings.

if (name.charAt(name.length() - 1) == region.charAt(region.length() - 1)) {
    System.out.println(name + "  " + region);
}

edit

I don't know why people decided to downvote, but I will add an explanation why I propose not to create one character long substrings.

if (name.substring(name.length() - 1).equals(region.substring(region.length()-1))

substring creates always a new String object. So here two new String objects (one for the last character of both strings) created only for the equality comparison. They are immediatly eligible for next possible garbage collection, as they are later not used.
The relevant part of the substring method return (beginIndex == 0) ? this : new String(value, beginIndex, subLen)

if (name.charAt(name.length() - 1) == region.charAt(region.length() - 1))

charAt returns a reference to the character at that position. So the equality comparision is done on a character of that string instance.
The relevant part of the charAt method return value[index] (value is char value[] in the String instance).

SubOptimal
  • 21,527
  • 3
  • 48
  • 60