aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Base Conversion

Chelsi Dennis
Greenhorn

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
Bartender

Joined: Oct 14, 2005
Posts: 18151
    
    8

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
Bartender

Joined: Oct 02, 2003
Posts: 10924
    
  12

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
Sheriff

Joined: Oct 13, 2005
Posts: 36575
    
  16
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 Manifest.mf 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

    C:\Users\Bykenhaal\Desktop\board>dir
    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 manifest.mf TicTacToeGUIGame.jar Board.class Cell.class Mark.
    class Outcome.class Player.class
    java.io.FileNotFoundException: manifest.mf (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at sun.tools.jar.Main.run(Main.java:171)
    at sun.tools.jar.Main.main(Main.java:1177)

    C:\Users\Bykenhaal\Desktop\board>C
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36575
        
      16
    Please continue all discussion in your original thread. I am blocking responses here.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Base Conversion
     
    Similar Threads
    Help with writing to a file!
    Storing variables in an array
    Newton's Method
    Convert decimal value to binary
    Binary to Decimal conversion