aspose file tools*
The moose likes Beginning Java and the fly likes String search/replace. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String search/replace." Watch "String search/replace." New topic
Author

String search/replace.

Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
OK, it's all cosmetic from here. The prog works & functions very well.
But... don't ya love it...
I want to strip the double quotes from the fields when I read it in & then put them around it when I write it back out. I also need to strip/replace 2 bang signs (!!) at the front of 3 of the fields. I read the strings into the string lineOfInput. I then use string test = lineOfInput.replace('"',' '), it compiles, but I get a runtime error...

C:\Dev\JavaProjects\Editmsg>java FileEdit
Exception in thread "main" java.lang.NullPointerException
at FileEdit.main(FileEdit.java:333)
333 is the line of the replace.
I just read the previous post on string replace & my syntax looks correct.

Thanks again, -Bill.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

readLine() returns null at end of file; you're probably already using this fact to break out of a loop. Just be sure the line isn't null before you do the replace.


[Jess in Action][AskingGoodQuestions]
Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
Earnest, thanks yet again. R U psychic, or WHAT? Without even seeing my code you knew I was using the != null test in my while loop. I think I'll need an additional book, the 2 I have are just not cutting it. Que's "JAVA Quick Reference", Michael Afergan, & the "Beginning Programming with Java for Dummies", Barry Burd, NEITHER tell me that the readline() returns a null at eof. Still, that's no excuse, I should have realized it by the test in the sample code. I'm going out this weekend & try something else. Thanks again. -Bill.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

No, I'm not psychic, just omniscient.
Bill Raterink
Greenhorn

Joined: Sep 03, 2003
Posts: 29
Oh Omnicient One... why does my output now have a 100 in front of it? I've taken your suggestion & the quotes & !! strip off easily. But when I try to put them back on, well... it's just what greenhorns do.
I suspect the char can't be "tacked onto" a string, but String or Byte didn't improve things any either. Here's a piece of my mess...

Is there a better way to do the ??? line? concat?, etc. I read the post on strings, but it was a bit over my head just yet. Thanks, -Bill.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
One way to fix it would be to change this:

to this:

Brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Exactly. The + operator only concats string; it adds everything else. Remember that a char is really a number behind the scenes. A Quote is ASCII 34 and a Bang is ASCII 33. When you say quote + bang + bang, you are really saying 33 + 33 + 34, which is 100. Then it adds the string (It converts the 100 to a string and then concats).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Ernest Friedman-Hill:
No, I'm not psychic, just omniscient.

I'm thinking more along the lines of omnipresent Ernest!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String search/replace.
 
Similar Threads
XML Parsing Error
Passing String from struts to javascript
Java Strings
Parsing a string with a e(circumflex) character
An invalid XML character Unicode: 0x19