Page 1 of 1

Convert data types

Posted: Fri Jun 20, 2003 1:51 pm
by Trinity
People.. help!!!!!

I'm having a little problem trying to convert String to int.

First I'm using a byte variable to read the input data.
byte data[ ] = new byte [3];
System.in.read (data);

Then I convert to String
String data_string = new String (data);

Then I convert to int
int data_int = Integer.parseInt(data_string)

Then appears a error , actually an exception <NumberFormatException>. If I use 'byte data[ ] = new byte[ 1 ]' doesn't make that error.
I don't know how to convert whitout making that exception.

Could someone help me please!

Thanx!
Trinity

Posted: Fri Jun 20, 2003 5:55 pm
by Adil
hi. i haven't tested it, but you might try trimming the "data_string" [ ie, data_string = data_string.trim() ] before you parse to Integer.

good luck, and hope this helps.

Posted: Sun Jun 22, 2003 3:17 pm
by ithamar
I dont know why are you reading the input like you do it.

I believe that its easier to read it like this

int n = Integer.parseInt(ReadLn(255).trim());

Where ReadLn is the method that you find here http://acm.uva.es/problemset/data/p100.java.html

Hope it helps