1
public static boolean retsim(int x, int y)
{
    String xx = x + "";
    String yy = y + "";
    boolean returnValue = false;
    if(xx.substring(xx.length() - 1) == yy.substring(yy.length() - 1))
    {
        returnValue = true;
    }
    return returnValue;
}

So when I compile and run this there are no errors. Yet it prints out 2 false statements, when there should be only one true or false statement. Like for example:

Enter in two sets of numbers with either,
The same or different end digit
7
7
// ^^ The two sevens are the inputs
false
false
// ^^ The two false statements that should only be printing out one

When the last digit is the same as the other last digit it should return true, and when those two digits are not the same the program should return false. Please help?!

public static boolean retsim(int x, int y)
{
    String xx = x + "";
    String yy = y + "";
    boolean returnValue = false;
    if(xx.substring(xx.length() - 1).equals(yy.substring(yy.length() - 1)))
    {
        returnValue = true;
    }
    return returnValue;
}

Now it returns:

Enter in two sets of numbers with either,
The same or different end digit
7
7
// ^^ The two sevens are the inputs
true
false
// ^^ The two false statements that should only be printing out one

Anyone have any ideas on how to get rid of that last false?

The code I use to call the class is:

public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Enter in two sets of numbers with either,\nThe same or different end digit");
int x2 = console.nextInt();
int y = console.nextInt();
System.out.println(retsim(x2,y));
}
Snare_Dragon
  • 59
  • 1
  • 11
  • terrible one but it's almost a solution: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java - you can make it a simple one-liner with http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java – zapl Oct 25 '16 at 23:55
  • Thanks for the suggestion, but do you have any idea why it would print out two false statements? @zapl – Snare_Dragon Oct 25 '16 at 23:58
  • first link, the == does not do what you think – zapl Oct 25 '16 at 23:59
  • so I should replace the == with a .equals()? @zapl – Snare_Dragon Oct 26 '16 at 00:00

2 Answers2

2

The very first comment from zapl already gives a great hint.

public static boolean retsim(int x, int y)
{
    return Math.abs(x)%10 == Math.abs(y)%10;
}
Dino Tw
  • 3,167
  • 4
  • 34
  • 48
-1

My guess is that your original implementation doesn't work because you're using the == operator to compare Strings when you should be using String#equals(Object). == is identity-equality. If you have two different String instances--even if they have the same contents--== will always return false.

How about using Integer#toString() instead?

public static boolean retsim(Integer x, Integer y) {
    if (null == x || null == y) {
         throw new IllegalArgumentException("x and y must not be null!");
    }

    String xString = x.toString();
    String yString = y.toString();

    return xString.charAt(xString.length-1) == xString.charAt(yString.length-1);
}

The return statement is working on two char primitives, in which case == is correct to use.

nasukkin
  • 2,460
  • 1
  • 12
  • 19
  • Sorry but could you explain what the: throw does? Im trying to follow what you did. @nasukkin – Snare_Dragon Oct 26 '16 at 00:04
  • 1
    @S.Nary Ahh, this is just argument verification. If `x` or `y` come into the method with the value `null`, an IllegalArgumentException will be thrown with a useful message explaining that the method can't operate on those arguments. – nasukkin Oct 26 '16 at 00:07