Author Topic: Random simple java programs that actually do something useful.  (Read 31310 times)

0 Members and 1 Guest are viewing this topic.

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #90 on: March 25, 2011, 05:52:00 pm »
Well, this is the java code that lets u convert decimal to binary ~ 36-base number.
Code: [Select]
import java.util.*;
public class Convert
{
    public static String convertbase(int n, int base) {
       // special case
       if (n == 0) return "0";

       String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String s = "";
       while (n > 0) {
          int d = n % base;
          s = digits.charAt(d) + s;
          n = n / base;
       }
       return s;
    }
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the number base 10 to convert:  ");
        int original = keyboard.nextInt();
        System.out.print("Enter the new base: ");
        int base = keyboard.nextInt();
        String answer = convertbase(original,base);
        System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]");
    }
}
Sig wipe!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #91 on: March 25, 2011, 05:59:46 pm »
Code: [Select]
import java.util.*;
public class Convert
{
    public static String convertbase(int n, int base) {
       // special case
       if (n == 0) return "0";

       String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String s = "";
       while (n > 0) {
          int d = n % base;
          s = digits.charAt(d) + s;
          n = n / base;
       }
       return s;
    }
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the number base 10 to convert:  ");
        int original = keyboard.nextInt();
        System.out.print("Enter the new base: ");
        int base = keyboard.nextInt();
        String answer = convertbase(original,base);
        System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]");
    }
}

This is looking good. It seems like it supports all bases.

Here's how it works for me:
Code: [Select]
david@DavidPC:~/Documentos/Java$ javac Convert.java
david@DavidPC:~/Documentos/Java$ java Convert
Enter the number base 10 to convert:  65535
Enter the new base: 16
The number 65535[base 10] = FFFF[base 16]
david@DavidPC:~/Documentos/Java$ java Convert
Enter the number base 10 to convert:  255
Enter the new base: 2
The number 255[base 10] = 11111111[base 2]
« Last Edit: March 25, 2011, 06:03:23 pm by Scout »

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #92 on: March 25, 2011, 06:14:38 pm »
Well, that's what supposed to look like.  :)
BTW, don't try incredibly huge number or it will go overflow XD
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #93 on: March 26, 2011, 03:49:09 pm »
then to go from any base to another...

Code: [Select]
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner intRead = new Scanner(System.in);
Scanner strRead = new Scanner(System.in);
System.out.print("Enter a number to convert: ");
String src = strRead.nextLine();
System.out.print("Enter the number's base: ");
int oldBase = intRead.nextInt();
System.out.print("Enter the desired base: ");
int newBase = intRead.nextInt();
String ans = Integer.toString(Integer.valueOf(src, oldBase), newBase);
System.out.println(src + " in base " + oldBase + " is " + ans + " in base " + newBase);
}
}