Results 1 to 7 of 7
  1. #1
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Using LineNumberReader and Recursion

    Hello,

    I have to use recursion and a LineNumberReader in my class to read a file and if the line contains the search keyword then give me the information for the line and the line number. But for some reason its giving me only one of the results.

    I have playing with it for a while and I noticed that the problem is that maybe my line numbers from the LineNumberReader are been incremented when passing it to the recursive method. I have attached a screenshot of the first time I call the procedure.
    Using LineNumberReader and Recursion-screeshot.png


    This the code I have.

    Java Code:
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.LineNumberReader;
    import javax.swing.JOptionPane;
    
    public class TestCaseForLineReader {
    
        public static void main(String[] args) throws IOException {
    
            String message = search();
            JOptionPane.showMessageDialog(null, message);
        }
    
        public static String search() throws IOException, FileNotFoundException {
    
            FileReader fReader;
            LineNumberReader lReader;
    
            fReader = new FileReader("C:\\Users\\Alfred\\Desktop\\New Text Document.txt");
            lReader = new LineNumberReader(fReader);
            String word = JOptionPane.showInputDialog(null, "Enter the work to search for.");
    
            if (!word.isEmpty()) {
                return searchLine(lReader, word);
            }
    
            //Return if no value.
            return "";
        }
    
        private static String searchLine(LineNumberReader lineReader, String keyWord) throws IOException {
    
            String line = lineReader.readLine();
    
            if (line == null) {
                return "";
            } else {
                if (line.contains(keyWord)) {
                    return lineReader.getLineNumber() + ": " + line + "\n"
                           + searchLine(lineReader, keyWord);
                } else {
                    return searchLine(lineReader, keyWord);
                }
            }
        }
    }
    Any ideas or comments?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Using LineNumberReader and Recursion

    Quote Originally Posted by fredlo2010 View Post
    Any ideas or comments?
    Yep, don't use recursion; simply read the entire file and register the line numbers of the lines that contain the String you''re looking for; a while loop can do the job.

    kind regards,'

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Using LineNumberReader and Recursion

    Quote Originally Posted by JosAH View Post
    Yep, don't use recursion; simply read the entire file and register the line numbers of the lines that contain the String you''re looking for; a while loop can do the job.

    kind regards,'

    Jos
    Thanks JosAH :) But unfortunately I have to use recursion; it's a requirement. :(

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Using LineNumberReader and Recursion

    Quote Originally Posted by fredlo2010 View Post
    Thanks JosAH :) But unfortunately I have to use recursion; it's a requirement. :(
    It's homework? What exactly goes wrong in your code? (a small example of the text input and its result might help a lot).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Using LineNumberReader and Recursion

    Oh wow I totally forgot to include the file.

    Here is the file : New Text Document.txt

    and the expected out come because all of those lines contain the word I am looking for "java"

    1: Java is the best language
    3: Because java
    4: jAvA

    Thanks

  6. #6
    2teez is offline Member
    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Default Re: Using LineNumberReader and Recursion

    Hi,
    ..because all of those lines contain the word I am looking for "java"
    Yes, they all do, but the string method contains that you are using will only "..Returns true if and only if this string contains the specified sequence of char values.". So the program is doing what you asked her to do, not what you have in mind.. :)

    Am sure you know that "java" is NOT the same as "jAvA" or "Java"..
    Instead of "java", use "Java" and "jAvA" and see what you will get as your result.

    So, how do you think you can make that work?
    Last edited by 2teez; 06-28-2015 at 12:26 AM.

  7. #7
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    28
    Rep Power
    0

    Default Re: Using LineNumberReader and Recursion

    lol :) there are moments like this when i feel really really silly :)

    @2teez: Thanks a lot for pointing out the obvious.

    I have fixed the code in the method searchLine and it's working like a charm. I added toLowerCase to both the search and the keyword.

    Java Code:
        private static String searchLine(LineNumberReader lineReader, String keyWord) throws IOException {
    
            String line = lineReader.readLine();
    
            if (line == null) {
                return "";
            } else {
                if (line.toLowerCase().contains(keyWord.toLowerCase())) {
                    return lineReader.getLineNumber() + ": " + line + "\n"
                           + searchLine(lineReader, keyWord);
                } else {
                    return searchLine(lineReader, keyWord);
                }
            }
        }
    Thanks a lot !!! :)

Similar Threads

  1. Recursion.
    By Cruncher in forum Advanced Java
    Replies: 30
    Last Post: 05-10-2010, 01:06 PM
  2. more fun... with recursion
    By sonny in forum New To Java
    Replies: 19
    Last Post: 03-23-2010, 05:09 AM
  3. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  4. Recursion
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:26 AM
  5. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:52 PM

Posting Permissions

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