java program help.. anyone pls? i have no idea on this.. im a n00b in java


Create a program that will input number in decimal number system &
convert it to the number system which the user entered

Sample Output:

Input number in decimal number system (base 10): 25
Convert it to what number system (2-16)? 2

25 in base 2 = 11001

Here is a way to do it.

Suppose we have an array: int[] newBase = new int[100];
it will hold the bits for the decimal system the user inputs.

Given 25 as decimal and 2 as base, do:
1. Compute ln(25)/ln(2) approx 4.64. Round it down to 4.

2. start a loop

int amountLeft=25;
for(int i = 4; i >=0; i--)
{
//see if that bit is being used and how much
int powerToTest = compute 2^i

//how many times does that power fit into what is left
int howManyTimesItFits = amountLeft/powerToTest;

//store in newBase
newBase = howManyTimesItFits;

//subtract away amount allocated to that bit
amountLeft-= powerToTest*howManyTimesItFits;
}

//by this point amountLeft should be zero and the bits should be in the newBase

For more programming help, visit www.NeedProgrammingHelp.com or email me at NeedProgrammingHelp@hotmail.com

Alex.

Try out this code, maybe you can use some parts of it

class Convert {
   
   public static void main(String args[]) {
   
    Scanner keyboard = new Scanner( System.in);
    String Num, ContCase = "y";
    int Base1, Base2;
   
    while(ContCase.equalsIgnoreCase("y"))
    {
       System.out.println("Enter your numbers base: ");
       Base1 = keyboard.readInt();
   
       if(Base1 > 10)
       {
           System.out.println("----------");
          System.out.println("| A = 10 |");
          System.out.println("| B = 11 |");
          System.out.println("| C = 12 |");
          System.out.println("| D = 13 |");
          System.out.println("| E = 14 |");
          System.out.println("----------");
       }
   
   
       System.out.println("Enter Number: ");
       Num = keyboard.nextString();
      
       System.out.println("Which base do you want to convert to: ");
       Base2 = keyboard.nextInt();
       
       Conversion( Base1, Num, Base2);
       
       System.out.println("Will you do Another Conversion:(y/n)");
       ContCase = keyboard.nextString();
    }   

}
   
    public static void Conversion(int Base1, String Num, int Base2){
   
    int k = 0, n = 0,total = 0, value = 0, totalnew = 0, z = 0;
    char c;
    String s, Num2 = "";
   
    for(n = 0; n <= Num.length()-1; n++)
    {
        c = Num.charAt(Num.length() - 1 - n);
       
       if(Character.isLetter(c))
       {
          if( c == 'A' || c == 'a')
             value=10;
           
           if( c == 'B' || c == 'b')
             value=11;
           
           if( c == 'C' || c == 'c')
             value=12;
           
           if( c == 'D' || c == 'd')
             value=13;
           
           if( c == 'E' || c == 'e') 
             value=14;
       }
       else
       {
          Character temp = new Character(c);           
           s = temp.toString();
           value = Integer.parseInt(s);
       }
       
       total += value * Math.pow(Base1,n);
       
    }   
     
     while(total > 0)
     {
        totalnew = total%Base2;
         
         if(totalnew > 9)
         {   
            if(totalnew == 10)
               Num2 += "A";
            
            if(totalnew == 11)
               Num2 += "B";
            
            if(totalnew == 12)
               Num2 += "C";
            
            if(totalnew == 13)
               Num2 += "D";
            
            if(totalnew == 14)
               Num2 += "E";           
         }
        else
           Num2 = Num2 + totalnew;
       
        total = total/Base2;
     }
         
         
     for(z = 0; z <= Num2.length()-1; z++)
     {
        System.out.print(Num2.charAt(Num2.length()-1-z));
     }
     System.out.println(); 
  }

}
This article has been dead for over six months. Start a new discussion instead.