Results 1 to 20 of 20
Thread: Reading a file
- 02-11-2010, 11:03 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
Reading a file
I'm having some trouble with this program, and any help would be great. I need a program to scan through a file, find a line that begins with a specific word, save that to a string, then keep scanning for another different keyword. If the second keyword is found before another line with the first the string should be printed, if the second keyword is not found before another line with the first the string should be cleared and the program should continue until the proper conditions are found. I can get it to find all of the lines with the first keyword, and return the proper number of iteration when attempting to deal with the second keyword, but the first line that meets the first parameter is returned and not the desired lines. I tried it a couple of different ways but I think that my general approach is flawed. I posted one of my attempts below. Sorry if that not clear, let me know if I can clear anything up. Thanks.
Java Code:import java.io.*; import java.util.*; public class InOut4 { public static void main(String[] args) throws IOException { Scanner infile=new Scanner(System.in); System.out.print("enter filename: "); String filename=infile.nextLine(); String out, line; File file=new File(filename); Scanner inputFile=new Scanner(file); while(inputFile.hasNext()) { line=inputFile.nextLine(); if(line.startsWith(" SCF")) { out=line; while(inputFile.hasNext()) { line=inputFile.nextLine(); if(line.startsWith(" Opt")) { System.out.println(out); } } } } } }
- 02-12-2010, 03:40 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For me the following it quite messup.
Once read a line you check a specific pattern there. If you find it only then move ahead to find the different pattern. Just think about the logic you've coded above. See how many time you use nextLine()Java Code:while(inputFile.hasNext()) { line=inputFile.nextLine(); if(line.startsWith(" SCF")) { out=line; while(inputFile.hasNext()) { line=inputFile.nextLine(); if(line.startsWith(" Opt")) { System.out.println(out); } } } }
- 02-12-2010, 04:07 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
Well that is indeed the problem. I got the outside loop to work fine, but I can't resolve a way to work out the rest. that's why I asked for help.
- 02-12-2010, 04:29 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Okay, once you find the pattern you want read the next line. And do the test. After that exit that root. You want to test only two patterns right?
- 02-12-2010, 04:34 AM #5
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
The problem is that I need the decision on whether or not the first test passes, and that string is printed, to be based on the second test. The only thing that needs to be output is the first line, but only if the second condition is met, as the file contains some lines that would pass the first but fail the second. Those lines would then not be output. I can conceptualize it, but I can't come up with the code.
- 02-12-2010, 04:54 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Try the following, check the code it's not test on IDE.
Java Code:while (inputFile.hasNext()) { line = inputFile.nextLine(); if (line.startsWith("a")) { //System.out.println(line); // Temp save out = line; if(inputFile.hasNext()) { line = inputFile.nextLine(); if (line.startsWith("t")) { System.out.println(out); } } else { System.out.println("EOF"); } } }
- 02-12-2010, 05:03 AM #7
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
Thanks for the thought. It runs, but doesn't return print anything to the screen. The thing is that the first occurrence of the first test might not meet the second condition. So it's reading the first line that meets the first test then looking for the second, not finding it and exiting the program. That's why I was trying to work it out with loops. Thank again for the help.
- 02-12-2010, 05:05 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to change the pattern accordingly. If you ca send the part of the file here to see, I'll let you know the pattern need to take on.
- 02-12-2010, 05:11 AM #9
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
I'm not sure that I follow. The file trying to be read is a large text file. I'll try to explain its formatting a bit better. So there are two keywords as previously discussed, A and B. A occurs at many points throughout the file. The only A's that I want to record are those that are followed by B's (though not directly) before another A comes about, and I need all of the A's in the files that meet those conditions. Sorry if I didn't explain it very well. Let me know if that helps.
- 02-12-2010, 05:29 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Okay, then you can see my code segment that I'm look in a line start with letter 'a' first. If it's found look in a line start with the letter 't'. You can change the content accordingly.
- 02-12-2010, 05:32 AM #11
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
I did change it already. The problem is that when it finds 'a' and doesn't find 't' is exits the program. The first 'a' may not be followed by 't', so it exits. The program should keep looking after the first 'a' was found not to be the correct 'a' but instead the program ends.
- 02-12-2010, 06:09 AM #12
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
In an attempt to further clarify, I'll try to generalize the format of the text file:
text
text
scan done 1
text
text
scan done 2
text
text
scan done 3
text
text
optimized
text
text
scan done 4
text
text
optimized
text
scan done 5
text
text
etc.
The lines containing scan 3 and scan 4 are the only ones that should be printed, as they are followed by the optimized keyword before another scan keyword is found. I hope that helps a bit.Last edited by fenwick; 02-12-2010 at 06:11 AM.
- 02-12-2010, 09:01 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No way, it cannot happen in the code segment I've given. There is a outer loop to carry on the process. Simply add the following content and run the code.
It print the following on the console.a line
two line
one line
two line
another line
actually another
ooops
another
that's all
I' don't know how you test the code.a line
another
- 02-12-2010, 04:03 PM #14
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
I know that it won't work that's still my problem. Either it won't print anything or it'll print the wrong numbers.
- 02-13-2010, 02:16 PM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-13-2010, 02:29 PM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Why don't you use the very simple logic then. If you write down the logic on a paper it's really easy to find the solution. ;)
Look at the following code segment.
Java Code:while (inputFile.hasNext()) { line = inputFile.nextLine(); if(line.startsWith("scan")) { // Temp save out = line; } if(line.startsWith("opt")) { System.out.println(out); } }
- 02-13-2010, 04:04 PM #17
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
The problem with that code is that the first occurrence of 'scan' is not followed by 'opt' so the program exits. It should keep looking until an occurrence of the correct sequence is found, 'scan' followed by 'opt' followed by 'scan', not 'scan' followed by 'scan'.
- 02-15-2010, 12:00 PM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try the last code segment I've send above?
- 02-15-2010, 03:24 PM #19
Member
- Join Date
- Feb 2010
- Posts
- 10
- Rep Power
- 0
Yes, but it's still having the problem described in my last post. It doesn't print anything as the program exits given that the first scan is not followed by opt.
- 02-22-2010, 04:01 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Now I'm not clear what you mean actually. I gave you a sample text file content, with a code segment. That sample text not follows the opt after the scan. In any pattern it works actually. I'm not clear what you really means.
Similar Threads
-
Reading a txt file
By diegosened in forum New To JavaReplies: 4Last Post: 01-27-2010, 01:05 PM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
reading a file
By new_coder in forum New To JavaReplies: 6Last Post: 08-19-2009, 03:59 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Reading a File
By zoom1017 in forum New To JavaReplies: 4Last Post: 02-20-2009, 03:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks