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
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: 35253
7
posted
0
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.
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
posted
0
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.