1

I know that 2389 % 10 is 9, but how could I create a method that takes 2 parameters? One for the number, and the other for the index and will return the value at index...

Textrus
  • 11
  • 1
  • 2
    Pls see this http://stackoverflow.com/questions/19194257/return-the-nth-digit-of-a-number – Sanchit Khera Jan 30 '17 at 01:33
  • 1
    For n'th digit counting by the right: divide by 10^(n-1) with / operator and then by 10 with % operator. – Aeteros Jan 30 '17 at 01:36
  • 1
    What does `2389` mean in mathematics? Are you familiar with the idea of "place values"? When you understand the mathematics behind representation of numbers, you should be able to do this pretty easily. – Code-Apprentice Jan 30 '17 at 01:36
  • In your question, do you mean that "2" would be index 0, "3" would be index 1, etc.? – steve Jan 30 '17 at 01:38

2 Answers2

3

You could do it using the charAt() method for a string:

public static int getNthDigit(int n, int pos) {
    return Character.getNumericValue(String.valueOf(n).charAt(pos))
}

Be careful: index start from 0. That means getNthDigit(1234,2) will return 3

You could ensure the pos number is in range before looking for it:

public static int getNthDigit(int n, int pos) {
    String toStr = String.valueOf(n)
    if (pos >= toStr.length() || pos < 0) {
        System.err.println("Bad index")
        return -1
    }
    return Character.getNumericValue(toStr.charAt(pos))
}
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • It worked for me... Can you tell me what is your issue then? (give me the parameter you entered and result) – Ulysse BN Jan 30 '17 at 01:39
  • It is not letting me parse to an int for some reason. – Textrus Jan 30 '17 at 01:40
  • Please give me the parameters you entered, and output you had – Ulysse BN Jan 30 '17 at 01:41
  • My question is: What value did you test your function against? For instance if I enter `getNthDigit(1234, 3)` I have a value of `4`. – Ulysse BN Jan 30 '17 at 01:46
  • I am testing the value 100 at pos 2. I get an error when trying to parse. – Textrus Jan 30 '17 at 01:51
  • Ok the error was because it was no string but character, i updated – Ulysse BN Jan 30 '17 at 01:54
  • Thank you, however when I test 100 with pos 3, it gives me an out of bounds error. – Textrus Jan 30 '17 at 01:57
  • This was one of my questions for my Java class, but when I give it the value of 123456789 and pos 4, it does not return 4. I need it to start on the right side. – Textrus Jan 30 '17 at 02:02
  • Stack Overflow is not a chat neither a code learning tool. Please have a **clear** question that indicates an error you encountered. To which I can answer. I’m not meant to guess your question, nor to talk with you in the comment section. – Ulysse BN Jan 30 '17 at 02:04
0
public static int getDigitAtIndex(int numberToExamine, int index) {
        if(index < 0) {
            throw new IndexOutOfBoundsException();
        }
        if(index == 0 && numberToExamine < 0) {
            throw new IllegalArgumentException();
        }
        String stringVersion = String.valueOf(numberToExamine);
        if(index >= stringVersion.length()) {
            throw new IndexOutOfBoundsException();
        }
        return Character.getNumericValue(stringVersion.charAt(index));
    }
User253489
  • 169
  • 1
  • 4