Results 1 to 8 of 8
  1. #1

    Default Help! Decimal to Binary problem in JAVA. (Beginner)


    Masters, nag himu ko sa amung assignment nga Converting Decimal to Binary.
    Nahimu na nko. pero naa lang koe problem. bali ang pag print sa Binary output.



    unxaon pag rearrange sa iyang Binary output?
    naay ni`ingun nko nga charAt() ra dw akng gamitun. pero d mn mada, akng giTry.. and d pa xd kaau ko khbaw mu use og charAt().


    patabang nya ko. please. or hatag lng mu advice.

    PS: beginner pa ko. so please, use basic codes pra maka sabot pd ko. hehe

  2. #2
    C.I.A. Platinum Member carmicael's Avatar
    Join Date
    Jul 2007
    Gender
    Male
    Posts
    2,142

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    there are a lot of solutions for this. using stacks or maybe a generic loop transfer algorithm. charAt() is also very applicable. use the Java API for discovering the use of charAt().

  3. #3

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    sakto ra man imong answer, pero sa loop nimo instead nga mag print ka dayon isulod lang nimo siya ug array then after sa loop kay i-reverse nimo ang array before i-print.

  4. #4

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    Kani ai para la kay kapareha

    [pre]
    public class DeciBinary {

    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("Please enter a decimal value: ");

    int decimal = s.nextInt();
    System.out.println("The binary value is: " + toBinaryString(decimal));

    }

    private static String toBinaryString(int i) {
    final char[] digits = {'0' , '1'};
    final int shift = 1;

    char[] buf = new char[32];
    int charPos = 32;
    int radix = 1 << shift;
    int mask = radix - 1;

    do {
    buf[--charPos] = digits[i & mask];
    i >>>= shift;
    } while (i != 0);

    return new String(buf, charPos, (32 - charPos));
    }

    }


    [/pre]

  5. #5

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    TS.
    ingon ako migo pwede ra nimo ma change ang int to Integer

    Integer a = s.nextInt();

    // to conver binary
    Integer.toBinaryString(a);
    // for hex
    Integer.toHexString(a);

  6. #6

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    sakto kaau na imo migo... mao na pinakasaun solution....

  7. #7

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    pwede raman pud dli mo use og array, imu ra e

    1. declare as string ang decimal nga input then
    2. get the character using strVariable.charAt(strLength) then
    3. parse the character using Integer.ParseInt( extractedStringCharacter )
    4. use a condition if extractedStringCharacter is divisible by 2 then binary value is equal to 1 else 0
    5. append the binary value in a string variable.


    hope this helps

  8. #8

    Default Re: Help! Decimal to Binary problem in JAVA. (Beginner)

    Quote Originally Posted by gwenharold View Post
    TS.
    ingon ako migo pwede ra nimo ma change ang int to Integer

    Integer a = s.nextInt();

    // to conver binary
    Integer.toBinaryString(a);
    // for hex
    Integer.toHexString(a);
    use this TS. sakto ni siya.

  9.    Advertisement

Similar Threads

 
  1. How to clear buffer in Java?
    By ares623 in forum Programming
    Replies: 17
    Last Post: 09-22-2009, 11:20 AM
  2. help:how to install linux in the computer
    By snakecobrapig in forum Alternative Systems
    Replies: 23
    Last Post: 12-25-2008, 03:43 AM
  3. NEED HELP - DVD to CD problem...
    By kazki in forum Software & Games (Old)
    Replies: 2
    Last Post: 02-04-2008, 01:31 PM
  4. Help! What to look for in a Video Card?
    By acronis in forum Computer Hardware
    Replies: 44
    Last Post: 04-14-2007, 10:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top