Help with Java program ***BEGINNER***

Discussion in 'Mac Programming' started by ZionSV, Mar 4, 2009.

  1. macrumors newbie

    Joined:
    Dec 1, 2008
    #1
    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


    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.. :\
     
  2. macrumors member

    Joined:
    Aug 5, 2003
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
    #2
    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
     
  3. thread starter macrumors newbie

    Joined:
    Dec 1, 2008
    #3
    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
     
  4. macrumors member

    Joined:
    Aug 5, 2003
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
    #4
    How about using an array of char's and using "output" as an index ...
     

Share This Page