Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 20-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 20-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java reading from a file"
Thread Thread Starter Forum Replies Last Post
Reading an XML file in Java Chetna Software Development 7 13-07-2011 04:31 PM
Problem in reading file in java TechGate Software Development 5 02-02-2010 03:05 AM
Reading tables in java.io Miles Runner Software Development 5 20-01-2010 10:30 AM
Java For Reading PDF Files Sheenas Software Development 5 18-01-2010 09:43 AM
Java - Reading from console WinDoWLoCuS Software Development 3 09-11-2009 12:18 PM


All times are GMT +5.5. The time now is 01:15 PM.