• Post Reply 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
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76868
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid that does not help at all. Avoid decimal format; the % tags are much easier to use.
 
Saloon Keeper
Posts: 5176
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Die Fledermaus does not fear such a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic