Results 1 to 10 of 10
- 08-15-2011, 12:25 PM #1
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
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
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:Hello 1 3 4 END
Here is my codeJava 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$
Can anyone help?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(); } }
Thanks
- 08-15-2011, 01:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Why don't you test for file.hasNext() in your last for-loop?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-15-2011, 01:18 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-15-2011, 01:24 PM #4
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
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
Unfortunaly this does not print the lines i want. It prints everything else but the lines i want.Java Code:for (int count = 0; file.hasNext(); count++) { line = file.nextLine(); if (count >= startLineNo && count <= endLineNo) { System.out.println(line); }
Thanks,
- 08-15-2011, 01:41 PM #5
Can you show the output that the program generates and also the contents of the file being read.It prints everything else but the lines i want.
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?
- 08-15-2011, 02:57 PM #6
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
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?
- 08-15-2011, 03:05 PM #7
You need to start from the beginning again. Close the file and open it again to use the numbers you found.i want to loop through file.txt only displaying lines 3 to 10.
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?
- 08-15-2011, 03:49 PM #8
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
Hi Norm,
This sounds good
but i simply dontknow how to implement this.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.
- 08-15-2011, 04:32 PM #9
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
After a long hard think, i came up with the following solution.... And it works
.gif)
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)); } } } }
- 08-15-2011, 05:28 PM #10
Similar Threads
-
Read specific lines
By AA++ in forum New To JavaReplies: 5Last Post: 06-01-2011, 06:11 AM -
reading lines from URL and printing backwards
By luxurymode in forum Advanced JavaReplies: 1Last Post: 03-30-2011, 08:18 AM -
writing a program to read a specific amount of lines from a file?
By welikedogs in forum New To JavaReplies: 4Last Post: 11-03-2010, 06:17 PM -
Help printing specific ArrayList elements
By CirKuT in forum New To JavaReplies: 5Last Post: 02-03-2009, 12:24 AM -
Reading specific lines
By ivvgangadhar in forum New To JavaReplies: 8Last Post: 01-12-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks