GeeCON Prague 2014*
The moose likes Java in General and the fly likes How to read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read "0000 0001" from keyboard?" Watch "How to read "0000 0001" from keyboard?" New topic
Author

How to read "0000 0001" from keyboard?

Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
Hello all,

I am trying to read an input from keyboard, but i wanted to use it a a byte data value. When i give "00000001" as input the program throws a NumberFormatException. I have posted the code that i have tried out. Can anyone help in this?



Thanks in advance.


Best regards,<br /> <br />vanchin
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Change your code to this:



Check and make sure that inputString is really a number, because if not, then parseInt won't be happy! Your subject line had a space in between the numbers...I think that would generate an exception.
siv sanny
Greenhorn

Joined: Jan 03, 2005
Posts: 8
Use this main code

public static void main (String[] a)
{
byte b=0;
String inputString;
try
{
byte[] input = new byte[100];
int count = 0;
System.out.print("Enter a byte value ->");
count = System.in.read(input);
inputString= convertToStrFrBytes(input,count-1);
b = Byte.parseByte(inputString,2);
}
catch (Exception e)
{e.printStackTrace();}


Now try with these changes. and make sure that the binary value you give as input is between -0111111 and 01111111 that is the range of a binary val. that a binary var can hold.

Thanx,
sankarvas
 
 
subject: How to read "0000 0001" from keyboard?