programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Octal to decimal using while loop

Greenhorn
Posts: 3
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
Hi Benjamin,
Decimals use a datatype of double or float. So you would have to convert from integer to a double

Ranch Hand
Posts: 934
4
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
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
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
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
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: 57437
175
I am afraid that does not help at all. Avoid decimal format; the % tags are much easier to use.

Master Rancher
Posts: 2290
76
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

 It is sorta covered in the JavaRanch Style Guide.