49

I need to define the last digit of a number assign this to value. After this, return the last digit.

My snippet of code doesn't work correctly...

Code:

public int lastDigit(int number) {
    String temp = Integer.toString(number);
    int[] guess = new int[temp.length()];
    int last = guess[temp.length() - 1];

    return last;
}

Question:

  • How to solve this issue?
catch23
  • 17,519
  • 42
  • 144
  • 217
  • 1
    Why does it not work? Do you get the wrong value or an exception? – Tomas McGuinness Jun 17 '13 at 10:09
  • 1
    You're creating an empty array when you create "guess". You need to populate it with the characters of temp too. The right way would be to use %10 as most people have answered below. But to fix your code without changing the approach, you can do something like `Integer.parseInt(temp.substring(temp.length()-1))` – Rajesh J Advani Jun 17 '13 at 10:31

12 Answers12

168

Just return (number % 10); i.e. take the modulus. This will be much faster than parsing in and out of a string.

If number can be negative then use (Math.abs(number) % 10);

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
18

Below is a simpler solution how to get the last digit from an int:

public int lastDigit(int number) { return Math.abs(number) % 10; }
SmushyTaco
  • 1,421
  • 2
  • 16
  • 32
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
8

Use

int lastDigit = number % 10. 

Read about Modulo operator: http://en.wikipedia.org/wiki/Modulo_operation

Or, if you want to go with your String solution

String charAtLastPosition = temp.charAt(temp.length()-1);
darijan
  • 9,725
  • 25
  • 38
5

No need to use any strings.Its over burden.

int i = 124;
int last= i%10;
System.out.println(last);   //prints 4
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

Without using '%'.

public int lastDigit(int no){
    int n1 = no / 10;
    n1 = no - n1 * 10;
    return n1;
}
1

You have just created an empty integer array. The array guess does not contain anything to my knowledge. The rest you should work out to get better.

Peter Jaloveczki
  • 2,039
  • 1
  • 19
  • 35
1

Your array don't have initialization. So it will give default value Zero. You can try like this also

String temp = Integer.toString(urNumber);
System.out.println(temp.charAt(temp.length()-1));
Sivaraman
  • 61
  • 7
1
public static void main(String[] args) {

    System.out.println(lastDigit(2347));
}

public static int lastDigit(int number)
{
    //your code goes here. 
    int last = number % 10;

    return last;
}

0/p:

7

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Manjunath
  • 169
  • 3
0

here is your method

public int lastDigit(int number)
{
    //your code goes here. 
    int last =number%10;
    return last;
}
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

Use StringUtils, in case you need string result:

String last = StringUtils.right(number.toString(), 1);
Andrew Kor
  • 121
  • 1
  • 1
  • 7
0

Another interesting way to do it which would also allow more than just the last number to be taken would be:

int number = 124454;
int overflow = (int)Math.floor(number/(1*10^n))*10^n;

int firstDigits = number - overflow;
//Where n is the number of numbers you wish to conserve</code>

In the above example if n was 1 then the program would return: 4

If n was 3 then the program would return 454

Samuel Newport
  • 194
  • 2
  • 11
-1

Although the best way to do this is to use % if you insist on using strings this will work

public int lastDigit(int number)
{
return Integer.parseInt(String.valueOf(Integer.toString(number).charAt(Integer.toString(number).length() - 1)));
}

but I just wrote this for completeness. Do not use this code. it is just awful.