PDA

View Full Version : Help with Java program ***BEGINNER***




ZionSV
Mar 5, 2009, 01:45 AM
Hello, I am in an intro to programming class and I'm stuck on a problem.

I am supposed to turn any number, i, into base k from 2 - 16

now I can do it with base 1 - 9 with this code



int i, k;
int v = 1;


System.out.print("\n\t Enter an integer: ");
keysIn = keyboard.next();
i = Integer.parseInt(keysIn);


System.out.print("\n\t Enter the base: ");
keysIn = keyboard.next();
k = Integer.parseInt(keysIn);


while (v <= i) {
v *= k;
}

while (v > 0) {

if (i < v) {
System.out.print(0);
}
else {

int output = (i - (i % v))/v;

System.out.print(output);

i -= v*output;
}

v = v/k;
}


The problem is I don't know how to use the letters A -F for base 10 - 16

Any help would be greatly appreciated, btw sorry this is such a noob question, I'm not very good at this yet.. :\



pstoehr
Mar 5, 2009, 04:26 AM
Hi,

it looks like you are looking for someone who should do your home work ;-)

But you get hint:
How about using an array with 16 elements that holds '0','1',...,'9','A',...'F'

Best regards
Peter

ZionSV
Mar 5, 2009, 04:46 AM
Hahaha just looking for some help is all :D

thanks, by the way! I haven't tried to use your hint yet but thanks in advance

pstoehr
Mar 5, 2009, 04:48 AM
How about using an array of char's and using "output" as an index ...