Question

In: Computer Science

Java Write a method that reads a text file and prints out the number of words...

Java

Write a method that reads a text file and prints out the number of words at the end of each line

Solutions

Expert Solution

  • Our program will read a file ,say Words.txt and print every line and number of words in that line after that.
  • Below is a Words.txt file ,java program  

// Words.txt file contents

Hello, my name
is Michael
Jackson. Welcome to
java programming
world

  

//Java code starts here

import java.io.BufferedReader; //imports required for file reading
import java.io.FileReader;

public class Main
{
//Method to print no. of words in every line
//Pass the filename as argument
public static void count_words(String filename) throws Exception
{String line; //String to store line
int count = 0; //Counter
  
//Opens a file in read mode
FileReader file = new FileReader(filename);
BufferedReader br = new BufferedReader(file);
  
//Gets each line till end of file is reached
while((line = br.readLine()) != null) {
//Let us print the line first
System.out.print(line+" ");
//Splits each line into words and store in words array
String words[] = line.split(" ");
//Print length each words[] i.e the number of words
System.out.println(words.length);
}

br.close(); //Close the reader
  
}
public static void main(String[] args) throws Exception {
count_words("Words.txt");
}
}


Related Solutions

● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
How many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT