• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use trim() with String.split(regx)?  RSS feed

 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Error message I get is:

trim() in java.lang.String can not be applied to (java.lang.String[])



Varuna
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message says it all.
split() returns an array of Strings. trim(), on the other hand, works on a single String object. You would need to iterate through the Array and then call trim() on the individual Array values.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A necessary skill for any programmer to develop is to read errors well and think on those lines, in some cases assume probable causes and dwell in to troubleshooting.

In this case the error is quite descriptive about the mistake you are making. Try to analyze the error message a bit harder. Try harder to figure out such errors by yourself..
 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured But previous error But in a instant like "This is a "" a Test" how can I discard the "" and display the only the words "This", "is", "a", "Test".
The method I coded with the intention of getting there is:-



But this returns even the ""

Varuna
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So inputLine is this: . Correct?
If so, then you'll want to split on: one or more white space characters or double quotes. You can do that by placing the white space character and the double quote character inside a character class, and making that character class greedy by placing a '+' sign after it:


[ December 09, 2008: Message edited by: Piet Verdriet ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tokens[x].trim() will never be equal to null. If there is only whitespace in the string then trim will return an empty string (a string of zero length).

Edit - this is just an FYI. Piet's solution is the way to go.
[ December 09, 2008: Message edited by: Joanne Neal ]
 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Piet Verdriet:
So inputLine is this: . Correct?
If so, then you'll want to split on: one or more white space characters or double quotes. You can do that by placing the white space character and the double quote character inside a character class, and making that character class greedy by placing a '+' sign after it:



[ December 09, 2008: Message edited by: Piet Verdriet ]


Hello Piet
The inputLine is "This is "" a Test".What I have to do in the program I am trying to create is, a User Enters a sentence, then I want to break that sentence into words comprised of letters and create another String Array, by discarding all other symbols like "" " , . ; : & * % $ # @ ! ` ~ or any other symbol other than a Letter.
What happens when you use + sign in the ?
What is meant by
making that character class greedy


Varuna
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, it seems you're not familiar with regex. Have a look at this tutorial:

http://java.sun.com/docs/books/tutorial/essential/regex/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!