Results 1 to 5 of 5
Thread: Trim() doesn't trim()
- 08-29-2011, 02:13 AM #1
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Trim() doesn't trim()
Hello,
I'm trying to read comma separated data but there are some blank spaces wreaking havoc. So I tried the trim() method to remove the spaces since from the book it looked straight forward. Not.
Here's what I thought would work (just to remove the spaces):
File file0 = new File("DataFile.txt");
Scanner readFromFile = new Scanner(file0);//open from file
String fileLineOld,
fileLineNew1;
while(readFromFile.hasNextLine())
{
fileLineOld = readFromFile.nextLine();//read from file
System.out.println(fileLineOld);
fileLineNew1 = fileLineOld.trim();
System.out.println(fileLineNew1);
}//end while
Here's the output:
(a space here)950, 624, 27.4, 21.4, 70, 6.0, 23.0,264, 4,305,308,298,356,17.13
950, 624, 27.4, 21.4, 70, 6.0, 23.0,264, 4,305,308,298,356,17.13
It gets rid of the first space but nothing else.
What am I missing.
Thanks for any help.
- 08-29-2011, 02:33 AM #2
What was in the DataFile.txt?
What do you expect the trim() method to do?
To see if there are leading or trailing spaces in a String when you print it add delimiters:
System.out.println(">"+ fileLineNew1 + "<");
- 08-29-2011, 02:37 AM #3
The trim method removes leading and trailing spaces. Nothing else. If you have spaces in the middle of the String then you need to handle them yourself.
Last edited by Junky; 08-29-2011 at 02:48 AM.
- 08-29-2011, 04:11 AM #4
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Oh brother. I was afraid you were going to say that...
Norm, fileLineOld has one record. I just used println to compare the differences. It was just an experiment to see what would work. Ultimately, I'll have to replace each record with "spaceless" records into a new file or rewrite to the original file (that makes me nervous even though I have many copies). And then do the other stuff.
Thanks guys.
- 08-29-2011, 07:55 AM #5
Similar Threads
-
Trim for null string
By laosu in forum New To JavaReplies: 3Last Post: 02-08-2011, 06:56 AM -
String.trim() method help.....
By arson09 in forum New To JavaReplies: 6Last Post: 04-23-2010, 01:19 AM -
How to trim the value in jrxml code
By Deepa in forum New To JavaReplies: 12Last Post: 01-03-2009, 08:27 AM -
How to trim text in viewer
By diva_garg in forum SWT / JFaceReplies: 4Last Post: 08-26-2008, 02:03 PM -
String trim
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks