• 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

How to read "0000 0001" from keyboard?

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic