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
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Octal to decimal using while loop

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 48
• Number of slices to send:
Optional 'thank-you' note:
Hi Benjamin,
Decimals use a datatype of double or float. So you would have to convert from integer to a double

Ranch Hand
Posts: 954
4
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the website. I have seen that before.
Like I said in OP, I understand the math. ie: :$k = \sum_{i=0}^n \left( a_i\times 8^i \right)$

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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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:

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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.

Marshal
Posts: 76868
366
• Number of slices to send:
Optional 'thank-you' note:
I am afraid that does not help at all. Avoid decimal format; the % tags are much easier to use.

Saloon Keeper
Posts: 5176
207
• Number of slices to send:
Optional 'thank-you' note:
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

 Die Fledermaus does not fear such a tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising