Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Readlines and find a match

    I am making 2 scripts to do some work for me.
    For one script I already made a topic.
    The reason why I make a 2nd topic for the other script is that they are a little bit similair. But through the huge amount of data I think 2 scripts will be better becose of the amount of time it would take to run them if combined.

    For this script I am reading in a file. And I make the script read the file line by line. Now are in the file "Test.txt" Words I am looking for. "Query=". There are lines that begin with this word.

    And I would like to know what function to use when to look for a specifick word.

    Eventualy I want the script to find the word "Query=" and remember the letter/number code behind it. And then look 5 lines lower to find another word match. And if there is a match to write the number to file1, and if no match to file2. And to do that for 5.000+ "Query=". (But I only want 2 output files. All who matched, and all who non matched)

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Did you look at the String API?

    And they're not "scripts" in Java. You write methods, which are in classes, which are in packages, which together make up your program.

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Almost

    I now use
    Java Code:
     if (line.indexOf("Query= ")>=0)
    My program does find the word I am looking for, but I what him to take the word after this word. That is the last thing I need to figure out. After that this one as good as finished.

    I tried .next() but that wasen't a real succes.
    But that should work right?

    For the rest this program works.
    And I know how to write it to a file.
    (But that part is not in it yet ofcourse.)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Try debugging your code by printing out the value of line before doing the if test. The printed output should help you understand.

  5. #5
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Still almost

    For some reason I only don't get the .next() to work.
    I also made a String for the number/code I want to remember. (number)

    Java Code:
     
    xD
    He does find the sentence with Query= in it, but now the rest.
    I tried number.next();.
    But he doesn't understand the . (error = cannot find symbol)
    It's frustrating, becose I think I am very close to capture the second word.
    Last edited by Lund01; 10-13-2010 at 08:55 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I only don't get the .next() to work.
    Where do you use the .next() in your code?

    Why the same tests in the if and else if
    Java Code:
                                     if (line.indexOf("Query= ")>=0) {
    					System.out.println("Found!");
    
    					//continue;
    				}
    				else if (line.indexOf("Query= ")>=0) {
    					continue;
    Where do you ever use the number variable?

  7. #7
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default ...

    I want the 'number' to be the word behind the word "Query= ".
    And the reason why if and else if are the same is becose I need to find a match 5 lines below, if yes write it to fileA. If there is no match he does the else if, where he needs to write it to fileB.

    Java Code:
     
    				if (line.indexOf("Query= ")>=0) {
    					System.out.println("Found!");
    					//String number2 = line.next(); 
    					/* number = s.next();
    					System.out.println(number); */
    
    					//continue;
    				}
    I used the .next() under the if code.
    And "String number2 = line.next();" was another thing I tried, but also no succes.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Will this work:
    Read the next line
    test if the line has the "query=" by using indexOf
    if the line does have it, use substring to get what follows "query=

  9. #9
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Substring

    A Substring works. :)
    Thanks for the help.
    I didn't know that substring yet.
    Last edited by Lund01; 10-04-2010 at 03:16 PM.

  10. #10
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Change

    This is the program what I have now.

    Java Code:
     
    xD
    I also made some changes.
    I deleated the if else, and made a dubble if loop.

    What I want with the second if loop is to look further in the file and see what we come by first. Do we see a line with ***** No hit found **** first or a line of "Query= " first.

    If I activate continue; it doesn't work.

    Or I would like the program to look 5 lines below the "Query= " for the line "***** No hit found ****".
    Maybe that would be more easy?
    Last edited by Lund01; 10-13-2010 at 08:56 AM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Can you add some comments to the code describing what it is supposed to do?

    Looking at the code you posted I have no idea what you want it to do. The comments and the code should be together.

  12. #12
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Explain code

    Sure, here it is.

    Java Code:
     
    xD
    Last edited by Lund01; 10-13-2010 at 08:56 AM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    To look at a line the n lines after a line, you need to read the intervening lines until you get to the nth following line.

  14. #14
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    And how do I do that?
    Becose this program finds all the "Query= " lines in the file. So he reads the whole file. But not specifick the lines under a found "Query= ".

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Pseudo code to read 5 lines from the file:
    for(int i=0; i < 5; i++) {
    Read a line from the file here
    }
    // here you will be at the 5th line following where you started from

  16. #16
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    instead of line.indexOf("Query= ") use line.trim().indexOf("Query= ")
    Ramya:cool:

  17. #17
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Code

    I now have this:

    Java Code:
     
    xD
    But he still won't see that 5 lines below there is a match.
    He only sees non-matches, even when there is a match.
    Now I am confused....
    Last edited by Lund01; 10-13-2010 at 08:56 AM.

  18. #18
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    Almost works. :p
    I added some more code so I can also write away the number (code thing I needed) to a file. Only that match thing from 5 lines below (***** No hits found ****) seems still always be false. There is still never a hit. And if I use line.matches then it just doesn't requonize the .match.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Try debugging your code by adding print outs of the lines as they are read with comments on the print out showing what is happening.
    System.out.println("skipping: " + line + "<"); // show the line being skipped

    Where in the for loop do you read the lines that are being skipped?

  20. #20
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default I see

    I see the problem, he still doesn't look 5 lines down.

    Java Code:
     
    					for(int i=0; i < 5; i++) {
    						//System.out.println("skipping: " + line + "<");
    
    					// Look 5 lines down. 
    						if (line.indexOf(NoHit)>=0) {
    							System.out.println("Test Secceed!");
    						}
    						else
    							//System.out.println("Wrong");
    							continue;
    							//output2b.println(number);
    					} // Too end: for(int i=0; i < 5; i++)
    It says 5 times:
    skipping: Query= iwqfcowqcw length=2<

Page 1 of 2 12 LastLast

Similar Threads

  1. Highlighting with exact match
    By ashkandaie in forum Lucene
    Replies: 3
    Last Post: 09-09-2010, 06:36 AM
  2. Match a word between any two HTML tags
    By mohammedsk in forum New To Java
    Replies: 7
    Last Post: 06-19-2010, 04:28 PM
  3. A crazy gui match
    By amarenash23 in forum New To Java
    Replies: 8
    Last Post: 12-30-2009, 03:39 PM
  4. Creating new instances of a Match over and over again
    By Che_Is_Alive in forum Advanced Java
    Replies: 2
    Last Post: 11-19-2009, 06:05 PM
  5. String match to the linkedlist element
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-06-2009, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •