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

Iterating through a String.

Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
Hi all,

I want to iterate through two Strings in such manner,any part of one String should be matched to any part of other String.For example-

String 1

1000,^[A-Z0-9_]+:[A-Z0-9_]+:The script failed to complete. See log file for details.$

String 2

1000,AZK43:GHJXH2:The script failed to complete. See log file for details.

what should I do such that comparison of both Strings should return true on the basis of match of phrase "failed to complete" or "log file" or anything else.

I don't want to use regular expressions, eg- for compilation of String1 such that it can match to String2.
How should I do it?

Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
Would anybody please suggest me anything???
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

can you elaborate?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

PatienceIsAVirtue. Seriously, you can't even wait 20 minutes for a reply?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
Example-

If there are two Strings

1.what I told you is correct.

2.is wrong whatever I told you.

See the bold parts of both Strings.that means I have to compare two strings on the basis of some specific set of characters in both Strings irrespective of their individual locations in both the Strings.
That means I want to perform some operation afterwards if a match is found.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

All I see as a requirement is that two strings be searched for a possible common third string. What am I missing?


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Extra information in this thread.
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
James you are not missing anything.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

James Sabre wrote:All I see as a requirement is that two strings be searched for a possible common third string. What am I missing?


I was missing the other thread.

It looks to me like the Ankit needs to check every rule against every message and if he re-opened the file used in the inner loop this would seem to work BUT BUT BUT this would mean going though the error messages file once for every line in the rules file. If the files are small then this is not likely to be a problem but if the files are even moderately large then this is likely to be very very slow.

My approach would be to try to hold the rules file in memory as a list of Patterns and then to go through this list once for each line of the error messages file. Still going to be slow but much faster than reading one of the files over and over again.

P.S. Ankit - please please please use meaningful names for your variables.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Alright Ankit, I have done some rough instruction for your problem, but you have to do some changes on this according to your prolem. also you need to improve the below logic.for example, you need to take care about the space between strings, etc..

is this fits you?
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1212
    
    1

@Ankit - this is reponse to earlier thread http://www.coderanch.com/t/541674/java/java/iterating-through-files




1. Reason for getting "Error in reading file from specified path String index out of range: -1" error is

you have extra blank lines at the end of "1002,^[A-Z0-9]+:[A-Z0-9]+:I failed to perform.$" line in Error_Rules_Demo.txt. Just remove those extra lines and you will not get this error.




2. Reason for not getting "I failed to perform" string in your code output is

br1 (instance of LineNumberReader for Error_Messages_latest_Demo.txt) is not getting reset in inner while loop after first error rule from Error_Rules_Demo.txt is read and completed.
Basically you have two while loop (one inside another). Outer loop is for Error rule file. Inner rule is for Error Messages file.
When Outer loop has read first error rule (^[A-Z0-9_]+:[A-Z0-9_]+:The script failed to complete. See log file for details.$) , it then iterates over list of Error Messages present in Error Messages txt file. So your counter br1 (instance of LineNumberReader for Error_Messages_latest_Demo.txt) has reached at 2 (means at "1002,A21:B35:I failed to perform.")
Now When Outer loop start its next cycle for second error rule (^[A-Z0-9]+:[A-Z0-9]+:I failed to perform.$) then your counter (LineNumberReader) br1 is increased to 3 and a null is returned in inner loop condition ( while((readLine2=br1.readLine())!=null)). Null is returned because br1 has reached end of file after first ieration of outer loop. Therefore your second error rule is totally ignored.

I have not used LineNumberReader but just checked Javadoc of it. This class do have reset() and mark() method. Try if you can manipulate br1 counter to get reset when first iteration is over.

Hope this explanation will be helpful for you

~ abhay
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
Hi,Abhay..thanks a lot for your reply..
you have extra blank lines at the end of "1002,^[A-Z0-9]+:[A-Z0-9]+:I failed to perform.$" line in Error_Rules_Demo.txt. Just remove those extra lines and you will not get this error.


I didn't get you.please elaborate it...


Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199

I have modified the class according to requirent.but still it's not working...
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Ankit Tripathi wrote:
I have modified the class according to requirent.but still it's not working...]


You are still not re-opening the file processed by the inner loop for each iteration of the outer loop. Once the inner loop file read reaches the end-of-file as it will do at the end of the first iteration of the outer loop it will never ever ever read anything but null so it MUST MUST MUST be closed then re-opened.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

I am fairly new at Java and Regex, but would it not be possible to write:



?

* i am not sure how this flag is really written.
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
is raf1.seek(0); not doing same job?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Ankit Tripathi wrote:is raf1.seek(0); not doing same job?


Only if it is done in the right place. The alignment of your braces is poor so it is not obvious to me it is in the right place. Reformat your code.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1212
    
    1

@Ankit -

Here is detailed description of solution of Error 1 in my earlier post

As you can see that there are two version of Error_Demo_File is mentioned. One version is incorrect and another is correct. Incorrect version has extra blank lines at the end of file. So you need to remove/delete these extra blank lines to avoid error

Incorrect Error_Demo_File.txt
ERROR_CODE,ERROR_RULE
1000,^[A-Z0-9_]+:[A-Z0-9_]+:The script failed to complete. See log file for details.$
1002,^[A-Z0-9]+:[A-Z0-9]+:I failed to perform.$




Correct Error_Demo_File.txt
ERROR_CODE,ERROR_RULE
1000,^[A-Z0-9_]+:[A-Z0-9_]+:The script failed to complete. See log file for details.$
1002,^[A-Z0-9]+:[A-Z0-9]+:I failed to perform.$
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1212
    
    1

@Ankit - Here is the correct code for your second error



Note that I have close streams for Error Messages file and re-opened it.

Output of this code is
AZK43:GHJXH2:The script failed to complete. See log file for details.
A21:B35:I failed to perform.


Hope this solution will help you

~ abhay
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 199
Hi all,

First of all thanks to all,specially to Abhay......

Actually my original problem consists of two problems

1.Iterating through files to read and write data in correct format.
2.Match Error codes with Error rules according to some regular pattern.previously I was denied to use regular expressions but afterwards this condition was vanished.....

now I am able to achieve both things using apache poi and regex.....

Here is the code...

>

I think it's time to close both threads.........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating through a String.