• 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
  • 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

converting to decimal but without using integer.parseInt() method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello guys!
so this is my first programming course and i have an assignment to convert a binary number entered by the user to a decimal. and my binary number should be stored in a a string.
and to check that all my digits are 1s and 0s i need to use a method to make sure it's valid and return true if the the number is correct and false otherwise.
so i did a search and saw that everyone was using the integer.parseInt(String , int radix) method to convert a binary string to int which worked completely fine, however, the only problem is we didn't take this with my professor so I'm not sure if it's OK and that maybe he wants another way to convert ? so my question is: how can i write this code in another way that doesn't use this method?.
here's my code:


this is my output:

enter a binary number to convert to decimal:
110101
the equivalent decimal value is: 53


thank you in advance
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd assume that your teacher doesn't want you using the parseInt method.

So how will you write a method to do it yourself? Well first, you need to sit down and think how you yourself would convert a binary number to decimal. How did you get 53 from 110101?

It might help if you wrote down the steps in English.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to just check for the index of "1" and the index of "0" in the string. Looking at the documentation for the methods of java.lang.String (← that's a link) will show the way.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a standard method for interconversions between binary and decimal. It has to do with ÷ 2 and × 2 but I can't remember how to do it just at the moment.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"So how will you write a method to do it yourself? Well first, you need to sit down and think how you yourself would convert a binary number to decimal. How did you get 53 from 110101?

It might help if you wrote down the steps in English. "

i understand that the right most digit has the value 2^0 so working my way to the left it would be: 110101 and taking the 1s only:
53 = 2^5 + 2^4 + 2^3 + 2^0

but i don't know how to write that in java ??

edit: i also saw people using math.pow thingy but we really didn't take that or how to actually write powers so i'm really confused.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sara james wrote:
i understand that the right most digit has the value 2^0 so working my way to the left it would be: 110101 and taking the 1s only:
53 = 2^5 + 2^4 + 2^3 + 2^0

but i don't know how to write that in java ??



You can do it with a loop, that is very similar to the one in the validBinary() method. Of course, you have a bit of math to do -- which will require calculating based on the index of the loop. I recommend working it out on paper first, as it will be much easier to envision when you draw it out.

Henry
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you iterate the String from right to left, I think the arithmetic becomes quite straightforward.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, it's easier to start with the leftmost non-zero bit in the string, and multiply an accumulator by two after adding the value of the bit to it, for each remaining bit.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds easy too. You probably don't need to look for non‑zero bits; it is probably easier to add 0 a few times if there are leading 0s.
You would need to convert from numbers in the String to bits, and one way to do it is to remember that a char is not a letter but a number and you can subtract two chars.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sara james,

Guys gave some useful hints. Did you come up with solution?
p.s. I've got mine (different one).

Show what you got so far, will try to help you.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic