| Author |
Ignoring spaces/multiple spaces in a txt file (replaceAll method problem)
|
Phil Maqui
Greenhorn
Joined: Oct 08, 2011
Posts: 6
|
|
Is this in the right thread? if not just transfer it to wherever, thanks
Hi i have been stuck on this problem for ages and already searched about the .split and replaceAll methods in the web and i didn't find any solution
Problem: To search for all records containing: "FAIL" from the input file (studentRecord.txt) and write all of the records to an output file (result.txt)
here is the studentRecord.txt just remake it yourself since its small and eazy to do anyways:
EDIT: put it in a code tag since forums wont read the extra spaces
Here's my Code:
So basically the problem is the spaces in the 4th token, i tried using replace all to replace all spaces in the 4th token with noting but it didn't work
|
https://www.warpportal.com/landing/ragnarok/registration.aspx?ref=5688322
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
It isn't clear what your problem is, but perhaps the trim() method of the String class might help with it?
|
 |
Phil Maqui
Greenhorn
Joined: Oct 08, 2011
Posts: 6
|
|
Oh yeah sorry i forgot to add this, the output file only contains and what i would like it to be is that the output file contains the lines that has a "FAIL" on it
Which brings back to the original problem that is how to remove the spaces and or just make the program read the word and determine if its equal to "FAIL" then write everything on that line into result.txt
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Okay. So "FAIL" isn't the same thing as " FAIL " because of the spaces. I'm sure you figured that out already. But you are comparing those to the string "FAIL" using the equals method, even though you actually don't want that. That's why I made the suggestion that I made.
|
 |
Phil Maqui
Greenhorn
Joined: Oct 08, 2011
Posts: 6
|
|
Ok so i did this:
and it still didn't work, trying to figure out what the error is i remove the result = result.trim(); in the meantime and added a System.out.prinln(result) so i can see what exactly is stored in there, i got:
FAIL
(nothing)
(nothing)
(nothing)
(nothing)
(nothing)
i dunno why this happens, if i recall correctly String[] st = s.split(" "); splits the value in the lines each time it encounters a " " so 1 A 90 FAIL becomes
1) 1
2) A
3) 90
4) FAIL
isn't supposed to be the same for the ones with spaces? like should be
1) 6
2) F
3) 25
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Phil Maqui wrote:isn't supposed to be the same for the ones with spaces?
You could certainly do some debugging to find out what's actually happening. But since you're splitting on spaces, what do you think will happen when you have, say, five spaces in a row?
|
 |
 |
|
|
subject: Ignoring spaces/multiple spaces in a txt file (replaceAll method problem)
|
|
|