Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Ignoring spaces/multiple spaces in a txt file (replaceAll method problem)  RSS feed

 
Phil Maqui
Greenhorn
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't clear what your problem is, but perhaps the trim() method of the String class might help with it?
 
Phil Maqui
Greenhorn
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!