Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: manual code for converting binary octa hexa and decimal.

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default manual code for converting binary octa hexa and decimal.

    //please help me with my project this will be graded tom. so heres the problem .. theres a main menu choosing binary,octa,hexa,and decimal if i choose decimal it will display decimal menu and then i will input the numbers of binary and then it automatically display the converting process for octa hexa and decimal. and if i choose octa .it display the octa menu and i will input the numbers in octa and then it automatically display the converting process for binary hexa and decimal.. and so on.. but in binary if i input 2 it will display invalid .. the same as octa hexa and decimal... in my code the only problem is the converting process. please help me. :/ our instructor said no one will use a converter for our project. we will code it manually ..so for a moment .. this is my code..


    import java.util.*;
    import java.lang.*;
    public class mainmenu {
    public static void main (String[]args)
    {
    Scanner in = new Scanner(System.in);
    char B, O, H, D, Q, L, N , Y;
    char bin , pk, pick ;
    int binary;
    int octa =0;
    int hexa =0;
    int decimal =0;
    double ans;


    do {
    System.out.println("\311\315\315\315\315\315\315\3 15\315\315\315\315\315\315\315\315\315\315\315\315 \315\273");
    System.out.println("\272 MAIN MENU \272");
    System.out.println("\272\t\t \272");
    System.out.println("\272 B]BINARY \272");
    System.out.println("\272 O]OCTA \272");
    System.out.println("\272 H] HEXA \272");
    System.out.println("\272 D]DECIMAL \272");
    System.out.println("\272 Q]QUIT \272");
    System.out.println("\272\t\t \272");
    System.out.println("\310\315\315\315\315\315\315\3 15\315\315\315\315\315\315\315\315\315\315\315\315 \315\274");
    System.out.println("Enter your choice? :");
    pick = in.next().charAt(0);
    pk = Character.toUpperCase(pick);


    switch (pk){
    case 'B' :
    System.out.println("\311\315\315\315\315\315\315\3 15\315\315\315\315\315\315\315\315\315\315\315\315 \315\273");
    System.out.println("\272 Binary Menu \272");
    System.out.println("\310\315\315\315\315\315\315\3 15\315\315\315\315\315\315\315\315\315\315\315\315 \315\274");
    System.out.println("enter binary numbers:");
    binary = in.nextInt();
    System.out.println("octa :" +octa);

    System.out.println("hexa: " +hexa);
    System.out.println("decimal :" +decimal);
    break;

    case 'O' :
    System.out.println("enter octa numbers:");
    binary = in.nextInt();
    System.out.println("octa :" +octa );
    System.out.println("hexa: " +hexa);
    System.out.println("decimal :" +decimal);
    break;

    case 'H' :
    System.out.println("enter hexa numbers:");
    binary = in.nextInt();
    System.out.println("octa :" +octa);
    System.out.println("hexa: " +hexa);
    System.out.println("decimal :" +decimal);
    break;

    case 'D' :
    System.out.println("enter decimal numbers:");
    binary = in.nextInt();
    System.out.println("octa :" +octa);
    System.out.println("hexa: " +hexa);
    System.out.println("decimal :" +decimal);
    break;

    case 'Q' :
    System.exit(0);
    break;
    default:
    System.out.println("no choice");
    }
    System.out.println("Do you want to continue? yes(Y) no(N)");
    L = in.next().charAt(0);
    switch(Character.toUpperCase(L))
    {

    case 'N':
    System.exit(0);
    break;
    case 'Y':
    System.out.println("");
    break;
    default:
    System.out.println("Invalid Input");
    System.exit(0);

    }

    }while(L != 'N');



    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: manual code for converting binary octa hexa and decimal.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Please copy the full contents of the console from when the program executes showing the problem.

    On Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: manual code for converting binary octa hexa and decimal.

    How would you, by hand, convert a value like "0000 0011" to decimal? To hex? What is your algorithm for doing so? One supposes you have been instructed in one of the long-hand ways of doing this...?

    Once you have an idea how to do this, write out the algorithm in English pseudo-code as a method that takes a binary value (hint: probably as a String you might read from the right-most position, number by number) and returns a nicely formatted decimal number as a String, suitable for display.

    Points for decorating the output string nicely and accepting Java literal conventions.

    e.g.:
    "0b1111111110110011" -> "65,459"

    A similar algorithm can be derived for generating octal and hex values.

    e.g:
    "0b1111111110110011" -> "0xFFB3"
    - > "0177663

    i.e., we can determine from the input what sort of number it is, and we should be able to determine (for example) that "0b0102" is illegal input even before trying to convert it. (See any reference on "Java literals" to see what how to format various literals.)

    Note that this does away with most of your interface boilerplate. Just accept a string, validate it as some specific base, and then pass it to the right method(s) to get your formatted result Strings.

    The devil is in the details regarding limits and error handling. But you need to start from the inside and work out from there.

Similar Threads

  1. Converting from decimal to hexadecimal
    By BTroj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2014, 09:55 PM
  2. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  3. Binary to Decimal Conversion
    By Java_boy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2012, 03:11 AM
  4. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM
  5. Decimal to binary conversion
    By baueml01 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 22nd, 2011, 06:42 AM