Results 1 to 5 of 5
  1. #1
    Gaboo is offline Member
    Join Date
    Jan 2015
    Posts
    2
    Rep Power
    0

    Default Having problem with base converting program

    Before I begin, I need to confess that this is indeed, for an assignment for class. However, I have worked quote hard on it, only to hit a brick wall.
    The idea behind this program is that the program prompts the user to input an initial base (2-36), which checks to ensure that it is a valid int, then asks for a number to convert (which is taken as a String), then it asks which desired base the user would like to convert said number to. I have a basic program that is not complete, but allows me to do a few conversions using convertTo. I believe that I am going to have abandon this method and try mathematically converting every number. This leads me to what I can and cannot do. I am unable to use the initialBase as a condition to know what kind of number i an converting. For instance, I don't know how to make program know that if "2" is the initialBase, that that means that the String is a binary number. THAT is what I'm having problems with.
    Here is my initial program that has a few things that are copied and pasted from other bits of my code in my program:
    XML Code:
    http://pastebin.com/6uu9pq7S
    I have broken down what I (think I) need to do here:
    1. Check to see if the input base is 2, 8, 10, 16, or 32. Hint: Put the possible bases in an array, and check the input base against the array.
    2. Check to see if the input number is valid for the base. Hint: Create a String "0123456789ABC...V" and compare each input character with the first "base" characters of the String.
    3. Check to see if the output base is 2, 8, 10, 16, or 32. Hint: Use the same possible bases array you used in step 1 to verify the input base.
    4. Check to see if the input base is equal to the output base. If so, print the input number.
    5. Perform a conversion from the input base to base 10.
    6. Perform a conversion from base 10 to the output base. You do this in 2 steps because it's easier to check each conversion separately.
    7. Output the converted number.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Having problem with base converting program

    What have you tried?

    if "2" is the initialBase, that that means that the String is a binary number. THAT is what I'm having problems with.
    I don't understand what that problem is. What is the problem about base 2 being binary or base 10 being decimal or base 16 being hexadecimal? There are probably names for lots of different bases.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Gaboo is offline Member
    Join Date
    Jan 2015
    Posts
    2
    Rep Power
    0

    Default Re: Having problem with base converting program

    Here is an example of what I need"
    "Enter a number to convert: "
    *user enters number
    "Enter what the base is of this number: "
    *user enters what base format the number is*
    "Now enter what base you'd like to convert this number to: "
    *user enters what base to convert number to*
    (conversion takes place behind the scenes)
    Converted number is printed

    What I am having problems with is how to make the program aware what base I am using initially. I can create a program where you enter a specific number in a require base (like hex) and have it convert it into say binary, but I don't know how to let the program know that something is initially a certain base and it know how to convert it to whatever provided finalBase is required.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Having problem with base converting program

    Quote Originally Posted by Gaboo View Post
    What I am having problems with is how to make the program aware what base I am using initially. I can create a program where you enter a specific number in a require base (like hex) and have it convert it into say binary, but I don't know how to let the program know that something is initially a certain base and it know how to convert it to whatever provided finalBase is required.
    Well you prompt for the input base and then the number, and then the output base. So let's assume you want to convert hex to binary.

    prompt for input base: 16
    input number: C7 (you also need to check that it conforms to base).
    output base: 2
    output number would be 11000111


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Having problem with base converting program

    Basically, you need to convert a String to an int (given a certain radix) and you need to convert from an int to a String (given (another) radix). If you setup your code like this:

    Java Code:
    int convertFrom(String input, int radix) { ... }
    String convertTo(int number, int radix) { ... }
    ... you're halfway there ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Problem with mbrola.base
    By arshadalisoomro in forum Advanced Java
    Replies: 26
    Last Post: 07-15-2012, 08:47 PM
  2. Help with converting to base 10 program
    By jsoeung in forum New To Java
    Replies: 2
    Last Post: 04-23-2012, 01:20 PM
  3. Converting Number to Base 8
    By nighteagle2 in forum New To Java
    Replies: 9
    Last Post: 02-17-2012, 02:38 AM
  4. Replies: 2
    Last Post: 11-27-2011, 05:11 PM
  5. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 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
  •