Question

write a method with the following signature that reads the file with specified filename and prints...

write a method with the following signature that reads the file with specified filename and prints out the number of words and the number of lines in the file

static void countWordsAndLines (string filename)

Homework Answers

Answer #1

If you have any doubts, please give me comment...

import java.util.*;

import java.io.*;

public class CountExcercise{

public static void main(String[] args) {

countWordsAndLines("test.txt");

}

static void countWordsAndLines(String filename){

String sentence, word;

int wordCount = 0, lineCount = 0;

try{

Scanner scnrFile = new Scanner(new File(filename));

while(scnrFile.hasNextLine()){

sentence = scnrFile.nextLine();

wordCount += sentence.split(" ").length;

lineCount ++;

}

scnrFile.close();

System.out.println("Number of words: "+wordCount);

System.out.println("Number of lines: "+lineCount);

}catch(FileNotFoundException e){

System.out.println("Unable to open file");

}

}

}

Know the answer?
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
Similar Questions
Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method...
Write a Hex Viewer method with the following header: private static void viewHex(String filename) The method reads bytes from filename and displays them in hex representation. The output should be formatted as in the example below, i.e., each line consists of 8 pairs of hex numbers, then ‘|’, then another 8 pairs. Use Integer.toHexString() to convert a byte into a string representing the equivalent hex. Use a try statement to handle IOException and display a simple error message if an...
The c++ question: Assume that there is a valid file cards.txt in the current directory which...
The c++ question: Assume that there is a valid file cards.txt in the current directory which contains 5 valid int values, each on its own line. Write a function which reads all the values from the file and prints out the message “Lucky 7!” to the screen for every 7 it sees. Your function must have the following signature: void printSevens();
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen
1. Assume there was a valid file called cards.txt in the current directory containing 5 valid...
1. Assume there was a valid file called cards.txt in the current directory containing 5 valid int values, each on its own line. It is needed to write the function that reads all values from file and then prints out: "lucky 7!" to the screen for every 7 it sees. Use the signature: void printSevens();
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen Extra Credit: Set a default argument of “intfile.txt” for the “ReadFile” function Write an overloaded function of “ReadFile” that accepts two filenames and reads an...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
Rearrange the following lines to produce a program that prints an image of a truck facing...
Rearrange the following lines to produce a program that prints an image of a truck facing left. System.out.println("+~: | |"); System.out.println(" o oo oo "); public class Truck System.out.println( " /-+--------------+"); System.out.println("+---+--------------+"); public static void main(String[] args) { { } }
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT