menu
QAmmunity.org
Login
Register
My account
Edit my Profile
Private messages
My favorites
Register
Ask a Question
Questions
Tags
Categories
Ask a Question
Write a program that outputs the number of characters, words, and lines in the files that are supplied as command-line arguments.
Dorab
asked
May 6, 2023
312,850
views
29
votes
29
votes
Write a program that outputs the number of characters, words, and lines in the files that are supplied as command-line arguments.
Computers and Technology
college
Dorab
asked
May 6, 2023
by
Dorab
2.7k
points
answer
comment
share this
share
0 Comments
Your comment on this question:
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Add comment
Cancel
Your answer
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Add answer
Cancel
1
Answer
9
votes
9
votes
public class Exercise_13 {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Invalid arguments.");
System.out.println("Usage: java Chapter_12.Exercise_13 filename");
System.exit(1);
}
File filename = new File(args[0]);
if (!filename.exists()) {
System.out.println(filename + " does not exist.");
System.exit(2);
}
int characters = 0;
int words = 0;
int lines = 0;
try {
Scanner input = new Scanner(filename);
while (input.hasNext()) {
String s = input.nextLine();
lines++;
characters += s.length();
String[] split = s.split(" ");
for (String word : split) {
words++;
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
System.out.println("Characters: " + characters);
System.out.println("Words: " + words);
System.out.println("Lines: " + lines);
}
}
John Skiles Skinner
answered
May 10, 2023
by
John Skiles Skinner
2.8k
points
ask related question
comment
share this
0 Comments
Your comment on this answer:
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Add comment
Cancel
Ask a Question
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.
1.6m
questions
2.0m
answers
Other Questions
Price of Tether coin right now? Don't answer if u don't know.
How many pokemon games have you all played i've played 14
Genres are useful for many reaseons. What are some explanations you can think of for how genres can be useful to players, game designers, and game publishers? this if for a video game design class worth
What does Al stand for? B. Artificial Ingenuity A. Actual Intelligence C. Artificial Intelligence D. Algorithm Intel
3. Suppose Recruitment and Hiring is one of the Users requirement of HumanResource Management System needs to perform in any organization or company.Suppose you are human resource managers of specific
Twitter
WhatsApp
Facebook
Reddit
LinkedIn
Email
Link Copied!
Copy
Search QAmmunity.org