Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Octal to decimal using while loop  RSS feed

 
Benjamin Doveralap
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the algorithm for converting Octal to decimal in a while loop. Taking any octal number up to 8 digits long and converting it to decimal,

I understand mathematically how it works. I am just having difficulty converted to java algorithm.

This is what I have so far:



after the while loop I am not sure what to do!!! Line 27
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Benjamin,
Decimals use a datatype of double or float. So you would have to convert from integer to a double
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin, this is not the way to convert octal to decimal. Check this wiki page.

Also class name should start with capital letter and variable names should be more meaningful.
 
Benjamin Doveralap
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the website. I have seen that before.
Like I said in OP, I understand the math. ie: :<math>k = \sum_{i=0}^n \left( a_i\times 8^i \right)</math>

The difficulty is using modulus in loop to achieve the decimal number. I will figure it out. Having a hard time though.
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin, if you are doing simple conversion from octet to decimal then why are you looping. Unless there is something in there am missing.

Check Converting to double
 
Benjamin Doveralap
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to convert a user input octal to decimal up to 8 integers and no digit greater than 8.

Here's what I have now:


 
Enos Winder
Greenhorn
Posts: 18
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you add the import for the decimal format?? import java.text.DecimalFormat

Finally to make a decimal from a regular number it requires multiplication. This can be accomplished with an if statement or even by itself. But where you are asking for a number you will need a string to hold that info then convert that to a double. that might require some parsing. But after all of that you should be just fine.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid that does not help at all. Avoid decimal format; the % tags are much easier to use.
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Benjamin,

do you know the difference between

and


Also, have a close look at line 3! And should you really return 'dec'?
(what about the naming of the variables 'dec' and 'oct'?)

Greetz,
Piet
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!