Ryanas
Ryanas

Reputation: 1827

check if first value of a string is different to the second value java

I have an app which returns the road that somebody is on and I would like to find out whether they are on a different road to the one they started on.

For example, I have

userLoc = address.getThoroughfare();

which gets their location and stores it, I would like to store that as a string to something like String firstLoc and then if(userLoc.equals(firstLoc))

Sorry if I was not clear enough, I am asking how to set a String to the first value of userLoc, so I may check it against in the future.

Upvotes: 0

Views: 329

Answers (1)

Chris Chambers
Chris Chambers

Reputation: 1365

I would recommend using equalsIgnoreCase(...) instead of equals to save yourself lots of headache down the road.

Basically, to flesh out your code a bit:

public boolean isStartingRoad(String loc){
 return loc.equalsIgnoreCase(firstLoc);
}

This of course assumes you are saving firstLoc as a global variable in the same class.

And then just call it passing userLoc to it like so

boolean sameroad = isStartingRoad(address.getThoroughfare());

Edit: Ok, let me give this another shot:

public boolean isStartingRoad(String loc){
     return loc.equalsIgnoreCase(address.getThoroughfare());
    }

Basically lets you pass a string for the current location and compares it.

Upvotes: 2

Related Questions