Question

JAVA: Make a single-threaded program that counts words in a text file and prints the result...

JAVA: Make a single-threaded program that counts words in a text file and prints the result in an output.txt

example:

if a input.txt file has a string "There was an apple, which was in there.", the output.txt should have:

there: 2

was : 2

an : 1

apple : 1

which :1

in : 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
try
{
//variable declaration
int wordCount;
String input = "";
String line = null;
  
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
  
while ((line = br.readLine()) != null)
{
input += line;
}
  
String inputLC = input.toLowerCase();
String[] words=inputLC.split(" ");
  
wordCount = 1;
  
for(int i=0;i<words.length;i++)
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
wordCount=wordCount+1;   
words[j]="0";
}
}
if(words[i]!="0")
{
//write into the output.txt file
writer.write(words[i]+" : "+wordCount+"\n");
}
wordCount=1;
}
writer.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}

OUTPUT:

output.txt

there : 2 was: 2 an : 1 apple, : 1 which : 1 in : 1

Add a comment
Know the answer?
Add Answer to:
JAVA: Make a single-threaded program that counts words in a text file and prints the result...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • JAVA: create a program that reads all text files in a directory, calculates the occurrences of...

    JAVA: create a program that reads all text files in a directory, calculates the occurrences of words, and saves the result in one single output text file. assume that you are in a directory called Desktop/programdirectory, where you have multiple text files. textfile1.txt : "how are you" textfile2.txt: "great thank you" textfile3.txt: "great to see you. see you later" Then, your output.txt should have: how: 1 are: 1 you: 4 great: 2 thank: 1 to: 1 see: 2 later: 1

  • python Create a program to open a text file for reading, find the maximum number in...

    python Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • Create a random access Java program to insert 3 missing words into a text file. You...

    Create a random access Java program to insert 3 missing words into a text file. You will be supplied with the missing words, which you may hard code in your program. You need to determine from the given text file where the words should be placed! Missing words are: details populate determine Randominput.txt Create a class to hold the x_x_x_x of a new vehicle (include make, model, type, color, number of doors and price as a minimum). Include an ___init___...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • How to write a Java program that reads the file "input.txt" and writes all even values...

    How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT