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 - 29 Sep 2005 03:59 GMT
> 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>
Roedy Green - 29 Sep 2005 06:59 GMT
>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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Michael - 29 Sep 2005 04:25 GMT
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 . . .
> 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.
jessu - 29 Sep 2005 05:21 GMT
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 - 29 Sep 2005 07:06 GMT
>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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 29 Sep 2005 05:09 GMT
>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.
s = s.trim();
To then convert to numbers. see
http://mindprod.com/applets/converter.html

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