aspose file tools*
The moose likes Beginning Java and the fly likes Binary to decimal converter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Binary to decimal converter" Watch "Binary to decimal converter" New topic
Author

Binary to decimal converter

Wayne Lam
Greenhorn

Joined: Feb 25, 2011
Posts: 6
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

Joined: Feb 25, 2011
Posts: 6
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

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.


You're on a gravy train with biscuit wheels Roy.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42622
    
  65
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.


Ping & DNS - my free Android networking tools app
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Doe's he not need ;

Integer.parseInt(input, 2);

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

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42622
    
  65
Either parseInt or valueOf will work, but you're right of course that it needs to be 2, not 16.
Wayne Lam
Greenhorn

Joined: Feb 25, 2011
Posts: 6
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

Joined: Feb 13, 2011
Posts: 33

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

Joined: Feb 25, 2011
Posts: 6
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

Joined: Feb 13, 2011
Posts: 33

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()
 
 
subject: Binary to decimal converter