Results 1 to 2 of 2
  1. #1
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default How to read a specific line and word in a txt file?

    I'm trying to mess around with file reading in java.

    I have a program that I want the user to input the file they want to read and then the word and sentence. I'm using 2 arraylists. One for the sentences and one for the words. The output would look like this...

    The text file would look like this..

    This is a test
    of the emergency progamming system.
    If this had been a real test, you have passed.


    So you can see the word is "the"

    and when you run the program the output would look something like this...

    What is the file to read from?
    datafile.txt
    Please enter the line number and word number(the first line is 1).
    2 2
    The word is: the



    I'm trying to build something like this, not sure how to come about this.


    This is what I have so far...

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    
    public class readingFile {
    
    	public static ArrayList<String> sentences = new ArrayList<String>();
    	public static ArrayList<Integer> words = new ArrayList<Integer>();
    	
    	
    	
    	
    	
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		System.out.println("What is the file to read from?");
    		 Scanner keyboard = new Scanner (System.in);
    	        String fileName = keyboard.next ();        
    	        Scanner inputStream = null;
    	        System.out.println ("The file " + fileName + "\n" +
    	                "contains the following lines:\n");
    	        try
    	        {
    	            inputStream = new Scanner (new File (fileName));
    	        }
    	        catch (FileNotFoundException e)
    	        {
    	            System.out.println ("Error opening the file " + fileName );
    	                    System.exit (0);
    	        }
    	        while (inputStream.hasNextLine ())
    	        {
    	            String line = inputStream.nextLine ();
    	            System.out.println (line);
    	        }
    	        inputStream.close ();
    	        
    	        
    	    }
    	
    
    	}

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: How to read a specific line and word in a txt file?

    [not efficiently!]
    you could use two for loops.
    the first is for the lines. iterate from 0 to (2-1 in your example) and call nextLine(). after that, the next nextLine() will switch the file pointer/scanner to the correct line.
    now you have to iterate to the correct word. iterate from 0 to (2-1 example) and call next(). after that, the next next() -call gives you the word that you are looking for

Similar Threads

  1. Read a specific line or a .txt
    By skaterboy987 in forum New To Java
    Replies: 2
    Last Post: 10-18-2011, 02:04 AM
  2. Replies: 3
    Last Post: 06-26-2011, 11:00 AM
  3. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 10:40 PM
  4. Counting specific word from a file
    By jaq in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 06:12 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 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
  •