• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating through a String.

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would anybody please suggest me anything???
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you elaborate?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PatienceIsAVirtue. Seriously, you can't even wait 20 minutes for a reply?
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I see as a requirement is that two strings be searched for a possible common third string. What am I missing?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extra information in this thread.
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James you are not missing anything.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ankit - this is reponse to earlier thread https://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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have modified the class according to requirent.but still it's not working...
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is raf1.seek(0); not doing same job?
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.........
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic