Trim() doesn't trim()
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
fileLineOld = readFromFile.nextLine();//read from file
fileLineNew1 = fileLineOld.trim();
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.
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 + "<");
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.
Oh brother. I was afraid you were going to say that...
Originally Posted by Junky
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.
Go through the Strnig API. I can think of two methods that can do what you want. One uses a regex, the other doesn't.