• 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

Binary to decimal converter

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys.got some problem in this program.Was suppose to write a program to get a binary string and display its value. Need to have 3 methods.

binaryToDecimal()
-Receive string which represents the value
-Converts the binary value to decimal
-Return results

validateInput()
-Receive string as parameter
-Return true if string contains 0s and 1s,false otherwise

main()
-Read input using scanner class
-Call validateInput() method to check whether inputted value is binary value,display error message and repeat input process
-call binaryToDecimal() method to get its converted decimal value and display it


had some problem in the validateInput() part. not sure is it correct
 
Wayne Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for your validateInput method you could method something like this,



Your current method will return every time you hit a 0 or 1 so it will only return false if the first character is not 1 or 0.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use Integer.valueOf(String, 16) and catch the exception.

I have edited your post to UseCodeTags; please do that in the future when posting code of an length. Otherwise it's unnecessarily hard to read the code, making it less likely that people will bother to do so.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doe's he not need ;

Integer.parseInt(input, 2);

Given that he's trying to parse binary not hex??

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either parseInt or valueOf will work, but you're right of course that it needs to be 2, not 16.
 
Wayne Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernie Mcracken wrote:for your validateInput method you could method something like this,



Your current method will return every time you hit a 0 or 1 so it will only return false if the first character is not 1 or 0.



Hi Ernie,after this,where should i call the method?
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you need to read your string using your Scanner then call the validateString() method



If you are going to have this in the same class as your Main then you'll need to make the validateString() method static.
 
Wayne Lam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernie Mcracken wrote:So you need to read your string using your Scanner then call the validateString() method



If you are going to have this in the same class as your Main then you'll need to make the validateString() method static.



Thanks Ernie again almost there,but now the problem is when i try to type "exit",the program doesn't exit.I need to display a message stating that "Please enter a proper binary number" when the validation returns false and allows the user to type another input.Appreciate the help as i quite new in programming.





 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just put a line in your do while loop that says if the string equals exit then exit the program, you can do this with System.exit()
reply
    Bookmark Topic Watch Topic
  • New Topic