Need java help

Discussion in 'OT Technology' started by meow, Jan 31, 2008.

  1. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,864
    Likes Received:
    72
    Location:
    PA
    edit: got some of it..scroll down
     
    Last edited: Jan 31, 2008
  2. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,864
    Likes Received:
    72
    Location:
    PA
    output should be like
    Code:
       
      [FONT=&quot]                 java Radix 235 16[/FONT]
      [FONT=&quot]                 235 in base 16 is EB
    
                     java Radix 235 2[/FONT]
      [FONT=&quot]                 235 in base 2 is 11101011[/FONT]
      
      [FONT=&quot]                 java Radix  1758 36[/FONT]
      [FONT=&quot]                 1758 in base 36 is 1CU[/FONT]
      
      [FONT=&quot]                 java Radix -50 8[/FONT]
      [FONT=&quot]                 Invalid decimal number: -50[/FONT]
      
      [FONT=&quot]                 java Radix 50 37[/FONT]
      [FONT=&quot]                 Invalid radix: 37[/FONT]
      
      [FONT=&quot]                 java Radix -50 37
                     Invalid decimal number: -50[/FONT]
      [FONT=&quot]                 Invalid radix: 37[/FONT]
      
     
    Last edited: Jan 31, 2008
  3. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    String answer = Long.toString(decimalNumber, radix);
    System.out.println(decimalNumber + " in base " + radix + " is " + answer);

    You don't need math shits, you just need to l2JavaDocs.

    It'll do all the conversion shit for you. The algorithm to convert the stuff is actually fairly in-depth, and has been the subject of a lot of research.
     
  4. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,864
    Likes Received:
    72
    Location:
    PA
    Code:
    public class Radix
    {
      public static void main(String[] args)
      {
        long decimalNumber, radix, x;
        int test;
        decimalNumber = Long.parseLong(args[0]);
        radix = Long.parseLong(args[1]);
        StringBuffer current = new StringBuffer();
        
        long decimalCopy = decimalNumber;
        
        do{
         x = decimalCopy%radix;
         decimalCopy = (decimalCopy/radix);
         current.append(x);
         
        }
        while(decimalCopy >= radix)
          ;
        current.append(decimalCopy);
        String total = current.toString();
        String revString = "" ;
        for(int i = total.length() -1 ; i >= 0 ; i--)
        {
          revString = revString + total.charAt(i);
        }
        if(decimalNumber < 0 && (radix < 2 || radix > 36)){
          System.out.println("Invalid decimal number: " + decimalNumber);
          System.out.println("Invalid radix: " + radix);}
        else if(decimalNumber < 0)
          System.out.println("Invalid decimal number: " + decimalNumber);
        else if (radix < 2 || radix > 36)
          System.out.println("Invalid radix: " + radix);
        else
          System.out.println(decimalNumber + " in base " + radix + " is " + revString);
        
      }
    }
    heres what i got now..it worked for base 2..but the ones with letters i cant do
     
  5. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,864
    Likes Received:
    72
    Location:
    PA
    actually, i think it works up to 9. Do i have to do switch statements to do like 15 = "F" or is there some easy way in java
     
  6. meow

    meow OT Supporter

    Joined:
    Feb 12, 2006
    Messages:
    11,864
    Likes Received:
    72
    Location:
    PA
    i got it all working on my own :big grin:
     

Share This Page