Convert data types

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
Trinity
New poster
Posts: 12
Joined: Tue Jun 10, 2003 3:40 pm

Convert data types

Post by Trinity » Fri Jun 20, 2003 1:51 pm

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

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil » Fri Jun 20, 2003 5:55 pm

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.

ithamar
Learning poster
Posts: 56
Joined: Mon May 13, 2002 11:58 pm
Location: Venezuela

Post by ithamar » Sun Jun 22, 2003 3:17 pm

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
Those Who Don't Know History are doomed to repeat it

Post Reply

Return to “Java”