wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String Replace Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Replace Problem" Watch "String Replace Problem" New topic
Author

String Replace Problem

Eric Paul
Greenhorn

Joined: May 02, 2009
Posts: 3
Hi, I'm rather new to Java. I would like to know why the following code doesn't work. I'm trying to parse the String named "string". My delimiters are topic, location, presenter and end. I try to loop through the string to grab all three instances (you'll notice there are three topics). However, after the second loop, it resets itself and gives me the same output as the first pass. Any ideas?


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
Welcome to JavaRanch.

I'll bet that the replaceAll statement does nothing, and so newStmt = string.

Try something like "string = string.substring(string.indexOf(end) + end.length())". Then you don't need "newStmt".


Ping & DNS - my free Android networking tools app
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40


Basically...

First, outside of the loop, you extract the first item, but you don't print it...

Next, when you enter the loop, you remove the first item from the string, and you extract the second item. This (second item) is the first item that will be printed.

Next, on the next iteration of the loop, you remove the second item from the string. However, this is the orginal string, so all three items are still present, so you extract the first item again. This (first item) is the second item that will be printed.

Next, on the next iteration of the loop, you remove the first item from the string. However, this is the orginal string, so all three items are still present, so you extract the second item again. This (second item) is the third item that will be printed.

If your loop kept going, your program will be toggling between the first and second items...

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Eric Paul
Greenhorn

Joined: May 02, 2009
Posts: 3
Ahh, I see now. So, I'm only removing a piece of the string at any time. I actually need to:

1. Print the line I selected
2. Completely delete that from the string
3. Print the next line
etc.

I'll give it a shot. I may have more questions.
Eric Paul
Greenhorn

Joined: May 02, 2009
Posts: 3
Hi guys, thanks for your help on this. This is how I ended up completing it. Probably not the most efficient way, but it works.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Replace Problem
 
Similar Threads
indexing and get particular word from the document
Can't get to next question...
BinarySearch() Question
Basic Java Help
I want to merge the two files into one of kind: by placing englishKeyword=English in format Russian