Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read "0000 0001" from keyboard?

 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic