Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Base Conversion

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
     
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Please continue all discussion in your original thread. I am blocking responses here.
     
    Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
      Bookmark Topic Watch Topic
    • New Topic