Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trim strings?

0 views
Skip to first unread message

Michael

unread,
Sep 28, 2005, 9:38:20 PM9/28/05
to
Hello everyone,

I just had a question. How do you trim strings? OR rather how do you insert
the command into java to trim strings? Since I have a program here that uses
many strings for entering data(numbers). But I get and exception numerica
runtime error.Thanks in advance.


Andrew Thompson

unread,
Sep 28, 2005, 10:59:51 PM9/28/05
to
Michael wrote:

> I just had a question. How do you trim strings?

Of what?

>..OR rather how do you insert

> the command into java to trim strings?

String.trim()

>..Since I have a program here that uses

> many strings for entering data(numbers). But I get and exception numerica
> runtime error.

You are talking gibberrish (also known as nonsense, rubbish, or crap)

As far as I understand, the number formatting methods are tolerant
of leading and trailing space.

Please copy/paste the exact errors or exceptions you are getting.

So that brings us to the questions,
a) What exactly are you doing? <http://www.physci.org/codes/sscce.jsp>
b) What data are you providing to the code?
c) What (exactly) happens when you run it?
<http://www.physci.org/codes/javafaq.jsp#exact>

Michael

unread,
Sep 28, 2005, 11:25:10 PM9/28/05
to
hello,
when i type this in i get this
Please enter 6 integers followed by a space.
1 5 79 86 4 5
Exception in thread "main" java.lang.NumberFormatException: 5 79 86 4 5
at java.lang.Integer.parseInt(Integer.java:423)
at java.lang.Integer.parseInt(Integer.java:463)
at change.main(change.java:61)
Press any key to continue . . .
"Michael" <mbia...@shaw.ca> wrote in message
news:gAH_e.8374$tl2.8340@pd7tw3no...

Roedy Green

unread,
Sep 29, 2005, 12:09:34 AM9/29/05
to
On Thu, 29 Sep 2005 01:38:20 GMT, "Michael" <mbia...@shaw.ca> wrote
or quoted :

s = s.trim();

To then convert to numbers. see
http://mindprod.com/applets/converter.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

jessu

unread,
Sep 29, 2005, 12:21:45 AM9/29/05
to
It seems you are trying to convert the String "1 5 79 86 4 5 " to
Integer.

You should first cut each of those numbers, and then convert it to
integer

Try something similar to this :

String input = "1 5 79 86 4 5 ";
String numbers[] = input.split(" ");

int aNumber;
for(int i=0; i<numbers.length; i++){
aNumber = Integer.parseInt ( numbers[i] ) ;
}


--------------
FeedFeeds : A new way to read news and blogs!
http://www.feedfeeds.com

Roedy Green

unread,
Sep 29, 2005, 1:59:42 AM9/29/05
to
On Thu, 29 Sep 2005 02:59:51 GMT, Andrew Thompson
<seemy...@www.invalid> wrote or quoted :

>
>As far as I understand, the number formatting methods are tolerant
>of leading and trailing space.

Integer.parselnt did not used to be. I have always been in the habit
of doing a trim() beforehand so I have not noticed if that has
changed.

Roedy Green

unread,
Sep 29, 2005, 2:06:46 AM9/29/05
to
On Thu, 29 Sep 2005 03:25:10 GMT, "Michael" <mbia...@shaw.ca> wrote
or quoted :

>when i type this in i get this


>Please enter 6 integers followed by a space.
>1 5 79 86 4 5

You have not shown us any code, so I have to guess what you wrote.
This is like refusing to disrobe at the doctor's.

Integer.parseInt is designed to handle only one number. It has no way
of handling six at once and cramming them all into one int.

So you have to spoon feed the numbers one at a time to it.

This means you somehow have to get 6 tiny Strings, each containing one
number, then feed them one at a time to Integer.parseInt, e.g. using a
loop that runs over the array of strings.

The easiest way to split them up is with the regex split method using
' ' as your splitter. See http://mindprod.com/jgloss/regex.html

You can also do it with a StringTokenizer or a charAt( i ) loop. Your
classmates have all posted this question several times earlier. You
might look for clues in those answers.


I am not going to show you the detailed answer since you would learn a
lot less that way.

0 new messages