Results 1 to 10 of 10
  1. #1
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default Printing to the screen specific lines from a file

    Hi,

    I have wrote this peice of code which takes 1 argument. The argument is a string and searches through a file and matches the string to a line in the file. For example

    file.txt
    Java Code:
    Hello
    1
    3
    4
    END
    with the arguemnt "Hello" the program should print from that line in the file until it finds the string "END". However i get the following error message.

    Java Code:
    bash-3.00$ java LogSearch "Hello"
    Argument exists at line: 1
    END exists at line: 5
    Exception in thread "main" java.util.NoSuchElementException: No line found
            at java.util.Scanner.nextLine(Scanner.java:1471)
            at LogSearch.main(LogSearch.java:45)
    bash-3.00$
    Here is my code

    Java Code:
    /**
     *
     * @author steven.haynes
     */
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    public class LogSearch {
    
        public static void main(String[] args) throws FileNotFoundException {
            
            Scanner file = new Scanner(new File("file.txt"));
            
            int startLineNo = 0;
            int endLineNo = 0;
            String line = "";
            
            while (file.hasNext()) {
                
                line = file.nextLine();
                
                startLineNo++;
                
                if (line.equals(args[0])) {
                    System.out.println("Argument exists at line: " + startLineNo);
                    break;
                }
            }
            
            for (int j = startLineNo; file.hasNext(); j++) {
                
                line = file.nextLine();
                
                endLineNo++;
                
                if (line.equals("END")) {
                    endLineNo = endLineNo + startLineNo;
                    System.out.println("END exists at line: " + endLineNo);
                    break;
                }
            }
            
            for (int i = startLineNo; i <= endLineNo; i++) {
                line = file.nextLine();
                System.out.println(line);
            }
            
            file.close();
        }
    }
    Can anyone help?

    Thanks

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

    Default

    Why don't you test for file.hasNext() in your last for-loop?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stevenhaynes5 View Post
    I hase the hasNext() methord to check if the file has anymore lines to read in it. Should this not be here? Please advise.
    I'm sorry, I don't see that test anywhere in your last loop (just before you close the file).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    Sorry i did not ready your answer correctly. I can not see a way on implementing the hasNext() methord without changing whats inside the for loop. However if have changed it to

    Java Code:
    for (int count = 0; file.hasNext(); count++) {
                line = file.nextLine();
                if (count >= startLineNo && count <= endLineNo) {
                    System.out.println(line);
                }
    Unfortunaly this does not print the lines i want. It prints everything else but the lines i want.

    Thanks,

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

    Default

    It prints everything else but the lines i want.
    Can you show the output that the program generates and also the contents of the file being read.

    Do you expect the reading to restart at the beginning of the file after you have read through it once?

    Try debugging your code by adding printlns to show the values of the variables as the change. You have a lot of integer variables that you set as you read the file. What are their values as the code executes?

  6. #6
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    Ok basically the program gets a starting position. This is the line number of where args[0] matchs line x in file.txt. It then looks for END in file.txt and stores that line number. Having a starting position line 3 for example and an ending position line 10 for example i want to loop through file.txt only displaying lines 3 to 10. Is there an easier way to do this?

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

    Default

    i want to loop through file.txt only displaying lines 3 to 10.
    You need to start from the beginning again. Close the file and open it again to use the numbers you found.

    Why read the file twice? Why not start displaying the lines when you find the start and stop displaying the lines when you find the end.
    Or could the end be missing and you don't want to display anything unless you have found the end?

  8. #8
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    Hi Norm,

    This sounds good
    Why read the file twice? Why not start displaying the lines when you find the start and stop displaying the lines when you find the end.
    but i simply dontknow how to implement this.

  9. #9
    stevenhaynes5 is offline Member
    Join Date
    May 2011
    Posts
    17
    Rep Power
    0

    Default

    After a long hard think, i came up with the following solution.... And it works

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    
    public class search {
        
        public static void main(String[] args) throws IOException {
            
            String line = null;
            String input = null;
            String exit = "END";
            
            input = args[0];
            
            Scanner file = new Scanner(new File("help_file"));
            
            while (file.hasNext()) {
                
                line = file.nextLine();
                
                if (line.contentEquals(input)) {
                    
                    do {
                        line = file.nextLine();
                        System.out.println(line);
                    } while (!line.equals(exit));
                }
            }
        }
    }

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

    Default

    After a long hard think
    Yes, that is how you have to do it sometimes. Some are easy, some are hard.

Similar Threads

  1. Read specific lines
    By AA++ in forum New To Java
    Replies: 5
    Last Post: 06-01-2011, 06:11 AM
  2. reading lines from URL and printing backwards
    By luxurymode in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2011, 08:18 AM
  3. Replies: 4
    Last Post: 11-03-2010, 06:17 PM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  5. Reading specific lines
    By ivvgangadhar in forum New To Java
    Replies: 8
    Last Post: 01-12-2009, 08:53 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
  •