Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Scanning Document Issue

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanning Document Issue

    Hi, this may seem like a basic question but I am a little new to this, so here goes:

    I need to scan a .txt file for a certain line of text, and if the text is contained in the file to output the number of times it occurs and the line of actual text in two seperate jtextfields.

    I am using the filereader class, should I be using the scanner class instead?

    thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scanning Document Issue

    Either will do the job.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Scanning Document Issue

    Hello redvenice. Welcome to the Java Programming Forums.

    You can easily use the Scanner class to do this. I have wrote a quick example for you.

    For the purpose of this example I have created a file called data.txt which contains:

    hello this is a test
    this is a test
    this is a test program
    my name is monkey
    javaprogrammingforums.com
    hello java
    test test test test
    javaprogrammingforums.com
    test test test test
    test test test test
    test test test test
    The line we are looking for in the data file is 'javaprogrammingforums.com'

    The code to do this is here:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class Redvenice {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
    	// line in data file to find
    	public static String findMe = "javaprogrammingforums.com";
    	public static int count = 0;
     
    	public static void main(String[] args) {
    		readFile();
    	}
     
    	public static void readFile() {
     
    		// Location of file to read
    		File file = new File("data.txt");
     
    		try {
    			Scanner scanner = new Scanner(file);
    			while (scanner.hasNextLine()) {
    				String line = scanner.nextLine();
    				// System.out.println(line);
    				if (line.equals(findMe)) {
    					// increase number of times found
    					count++;
    					// update jtextfields here
    					System.out.println("FOUND - " + line);
    				}
    			}
    			System.out.println(findMe + " found " + count + " times");
    			scanner.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
    	}
     
    }

    The output is this:

    FOUND - javaprogrammingforums.com
    FOUND - javaprogrammingforums.com
    javaprogrammingforums.com found 2 times
    I hope this helps solve your issues
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Upload Image / Document
    By systech44 in forum Java Servlet
    Replies: 1
    Last Post: August 11th, 2010, 09:42 AM
  2. Exception while parsing jasper document
    By rajumtmm in forum Member Introductions
    Replies: 0
    Last Post: February 26th, 2010, 05:44 AM
  3. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  4. Problems with XPathExpression on memory built Document
    By JRSofty in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 12th, 2009, 08:18 PM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM