Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2005

Tip: Looking for answers? Try searching our database.

Trim strings?

Thread view: 
Michael - 29 Sep 2005 02:38 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.