Results 1 to 20 of 20

Thread: Reading a file

  1. #1
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default 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);
    					}
    
    				}
    			}
    		}
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    For me the following it quite messup.

    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);
    					}
    
    				}
    			}
    		}
    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()

  3. #3
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  5. #5
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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");
                    }
                }
            }

  7. #7
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  9. #9
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  11. #11
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  12. #12
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by fenwick View Post
    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.
    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.

    a line
    two line
    one line
    two line
    another line
    actually another
    ooops
    another
    that's all
    It print the following on the console.

    a line
    another
    I' don't know how you test the code.

  14. #14
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by fenwick View Post
    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.
    I think miss-understood the requirement. You don't want to test the consecutive lines, right?

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by fenwick View Post
    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.
    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);
                }
            }

  17. #17
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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'.

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  19. #19
    fenwick is offline Member
    Join Date
    Feb 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Reading a txt file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-27-2010, 01:05 PM
  2. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  3. reading a file
    By new_coder in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 03:59 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Reading a File
    By zoom1017 in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 03:04 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
  •