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:07

>>1
Please, fuck off, we had too much ``plz halp'' threads. Go to /g/ and ask them.

Name: OP !!axoA77fHn6pVPtG 2011-01-23 18:10

>>2
OP here: I already asked /g/ that's why I'm here. :(

Why so angry?

Name: Anonymous 2011-01-23 18:14

>>1
public static int getDigit(int number, int index) {
  if (index == 0)
    return number%10;
  else
    return getDigit(number/10, index-1);
}

Name: Anonymous 2011-01-23 18:15

>>3
Scroll the frontpage and see how much shitstorm we already had today.

Your getDigit seems pointless, you could just return (the indexth digit of this.number).toString(). You could get it by using integer division, modulo and a loop.

Name: >>5 2011-01-23 18:15

>>5
You could get it by using integer division, modulo and a loop.
>>4
Ok, thread over.

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;
}

Name: OP !!axoA77fHn6pVPtG 2011-01-23 18:24

Thanks guys. I didn't mean to bother about homework help. In truth, I haven't been on 4chan in well over a year.

Name: Anonymous 2011-01-23 18:27

>>8
In truth, we are not part of ``4chan'' and abhor the imageboards.

Name: Anonymous 2011-01-23 19:04

riders of the /prog/

Name: Anonymous 2011-01-23 19:55

>>7

; ((and/c rational? inexact?) exact-integer? . -> . integer?)
(define (get-digit m n) (remainder (round (/ m (expt 10 n))) 10))

(define x 123.221)
(define y -123.332)
(get-digit x 0) ; 3
(get-digit x 1) ; 2
(get-digit x -1) ; 2
(get-digit y 0) ; -3
(get-digit y -1) ; -3
(get-digit y -4) ; 0, because the -4th position IS zero.
(get-digit y 4) ; same

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