Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java help

Name: Anonymous 2011-01-23 18:05

Hey /prog/, I haven't been here in a while and I was wondering if you guys could offer some basic programming help.

I'm writing a program that parses integers and returns the word, i.e. 100 yeilds "one hundred." Obviously I'm not going to post all of the code. I'm only having trouble implementing this one method.

//Return any single digit of the instance variable
//Parameter index gives the position of the required digit
//index 0 indicates the least significant digit
//Example: 7046
//Digit-0 = 6, Digit-1 = 4, Digit-2 = 0, Digit-3 = 7
//All higher-indexed (4, 5, 6, ...) digits are 0
//A RuntimeException is thrown if parameter index < 0
//
public int getDigit(int index)
{
  String numStr = Integer.toString(this.number);
  if(index < 0)
    throw new RuntimeException("Invalid index");
  else //wtf do I do?
}//end of getDigit

this.number is a private class instance variable

What throws me off is that the indexes are backwards.
tl;dr noob needs java help

Name: Anonymous 2011-01-23 18:20

>>4
Prettified for Javatards:

public int getDigit(int index) {
   if (index < 0) throw new RuntimeException("Invalid index");
   int n = this.number;
   while(index-->=0) n/=10;
   return n%10;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List