This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Base Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Base Conversion

Chelsi Dennis

Joined: May 05, 2013
Posts: 1
I am writing a program to convert a base 10 integer to any base 2-16. Here are the terms:

"The method is to convert the decimal value the user selected into whatever base the user selected and print the converted value one place value at a time by using an index into an array of characters 0-9 amd A-F that is initialized to contain those characters.
In the method you will find the largest place value (i.e. power of the base) that will divide into the decimal number.
Then you can set up a loop that will operate from that power down to and including the 0th power to determine how many times each place value goes into the decimal number. Using the loop counter, index into the character array to print the character that corresponds to the quotient number and then subtract the product of the place value and the quotient from the decimal number. With the power (loop index) decreased, repeat until you finish the 0th place value.

Here's what I have so far:

I think I have it set up correctly, I'm just not sure how to attack it. I understand the concept of what I'm doing, just not the execution of it.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19727

You might find it useful to work through some examples yourself, just with pencil and paper. For example you could take the number 3907 and convert it to base 7 using the method you described there. (Which isn't the best method, but that doesn't matter because you have to use the method you described.) Try several different examples, just picking random inputs and bases. Make sure you watch what you're doing and try to match it to the code you have there.

And don't be too attached to that code. If you find it doesn't correspond to what your pencil is doing, then don't hesitate to toss it out and replace it with something more suitable.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11858

comments like "end main" are really not needed. If you use proper indentation, it is obvious where each method/loop ends and you don't have the clutter of those comments. Check this out:

It's easy to tell at a glance where things start and stop. also, proper formatting makes it easier to find the extra curly brace you have in your code.

Having that extra brace tells me you are not compiling often enough. NEVER write more than 2-3 lines of code before you re-compile. It is much easier to find and fix a problem if you've added 3 lines than if you've added 30.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43970
Welcome to the Ranch

Another bit of good advice, but very counter‑intuitive.
You will find your indentation and brace matching much easier of you get a decent text editor (eg gedit, kate, NotePad2, jEdit, NotePad++) and select its options to include:-
  • 1: Bracket matching.
  • 2: Syntax highlighting (you can probably change the colours to suit yourself).
  • 3: Automatic indentation
  • 4: Automatic conversion of 1 tab → 4 spaces
  • 5: Right margin warning at not more than 120 characters
  • The NotePad program which comes with Windows® is a very poor tool for programming, because it does not support those options (or didn’t when I last tried it), and has some nasty habits like adding .txt to files while you are not watching.
    Once you have those features working, the most counter‑intuitive thing of all, is that you write much of your code backwards
    Brian Bykenhaal
    Ranch Hand

    Joined: Dec 17, 2013
    Posts: 57
    I tried to make the .jar again this time I used and come up with the following any ideas?

    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.

    C:\Users\Bykenhaal\Desktop>cd board

    Volume in drive C is Windows
    Volume Serial Number is 9A73-751D

    Directory of C:\Users\Bykenhaal\Desktop\board

    02/06/2014 01:14 PM <DIR> .
    02/06/2014 01:14 PM <DIR> ..
    02/06/2014 01:07 PM 2,431 Board.class
    02/05/2014 06:06 PM 1,702 Cell.class
    02/04/2014 03:47 PM 1,195 Mark.class
    02/04/2014 03:47 PM 960 Outcome.class
    02/04/2014 03:47 PM 832 Player.class
    02/05/2014 06:06 PM 3,313 TictacToeGUIGame.class
    02/06/2014 01:13 PM 6,385 TicTacToeGUIGame.jar
    7 File(s) 16,818 bytes
    2 Dir(s) 1,909,128,675,328 bytes free

    C:\Users\Bykenhaal\Desktop\board>jar cmf TicTacToeGUIGame.jar Board.class Cell.class Mark.
    class Outcome.class Player.class (The system cannot find the file specified)
    at Method)

    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 43970
    Please continue all discussion in your original thread. I am blocking responses here.
    I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
    subject: Base Conversion
    It's not a secret anymore!