Results 1 to 2 of 2

Thread: Java reading from a file

  1. #1
    Join Date
    Nov 2009
    Posts
    1

    Java reading from a file

    First I have my text file called "input.txt"
    For example, the file contains the following:
    Hi
    Hello
    Bye
    HELLO
    GoodBye
    Hi
    hello

    I am trying to write a java program that first asks the user to enter a word (any word), read as a String. The program should keep asking for a word until the user enters -1 (also read as a String).
    Once a word has been entered, my program must go in the file input.txt and count the number of times the word appears (this could be zero – 0 if the word does not appear in the file). You must go through the entire file to get a complete count. Once I know the final count for a given word, output the results in a file using the following format (as an example):

    Monitor 2

    Comparisons between words inputted from the user and words in Lab7.txt should be case insensitive. However, words printed in the output file should be the same as originally entered by users (i.e., case sensitive).

    P.S. I must use only two loops to complete this lab. One main loop that keeps
    asking the user for a word and prints the results in the output file. Another loop, within the main loop, to go through the file Lab7.txt and keep track of the count. I will have to reset the cursor in the file Lab7.txt before reading a new word from the user. To do this, close the file and re-open it again.

    Example:
    Please enter a keyword (enter -1 to exit): Hi
    Please enter a keyword (enter -1 to exit): Hello
    Please enter a keyword (enter -1 to exit): Good
    Please enter a keyword (enter -1 to exit): HELLO
    Please enter a keyword (enter -1 to exit): -1

    Output.txt:
    Hi 2
    Hello 2
    Good 0
    HELLO 1

    The following is my code but Then I get stuck after I ask the user to enter a keyword...

    import java.io.PrintStream;
    import java.util.Scanner;
    import java.lang.String;
    import java.io.File;

    public class readAFile{

    public static void main(String[] args)throws java.io.IOException{

    Scanner input = new Scanner(System.in);
    PrintStream output = System.out;
    Scanner fileInput = new Scanner(new File("input.txt"));
    PrintStream fileOutput = new PrintStream(new File("output.txt"));

    int i = -1;
    String exit = i + "";;
    int count = 0;

    System.out.print("Please enter a keyword (enter -1 to exit): ");
    for (String word = input.nextLine(); !word.equals(exit); word = input.nextLine()){
    System.out.print("Please enter a keyword (enter -1 to exit): ");
    for (int x = 0; fileInput.hasNextLine(); x++){


    }
    }
    Last edited by xqc72; 20-11-2009 at 04:54 AM.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Java reading from a file

    Stand-alone applications written in Java can access the local file-system, but applets executing under the control of a browser cannot. First, open a FileReader, then filter this through a BufferedReader to read the file one line at a time. An application uses a data output stream to write data that can later be read by a data input stream.

Similar Threads

  1. Reading an XML file in Java
    By Chetna in forum Software Development
    Replies: 7
    Last Post: 13-07-2011, 04:31 PM
  2. Problem in reading file in java
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 03:05 AM
  3. Reading tables in java.io
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:30 AM
  4. Java For Reading PDF Files
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 09:43 AM
  5. Java - Reading from console
    By WinDoWLoCuS in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 12:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,704,331.80637 seconds with 17 queries