Write an app. that asks the user for the name of a .txt file and then tells them
ID: 3581724 • Letter: W
Question
Write an app. that asks the user for the name of a .txt file and then tells them how many words the file contains, how many characters it contains, and also how many words it contains that begin with a capital letter. You should create your sample .txt file using a simple text editor such as Windows Notepad (find it under Start, All Programs, Accessories). Hint: use a String Tokenizer in this app. A sample .txt file to use as input might contain something like: one two three forty-six Dogs, Cats, Bats Please do not user bufferread, try, catch, or break statement.
USE these to write a text file: for example
PrintWriter outputFile = new PrintWriter("StudentData.txt");
PrintWriter outputFile = new PrintWriter("Names.txt");
outputFile.println("Chris");
outputFile.println("Kathryn");
outputFile.println("Jean");
outputFile.close();
•FileWriter fw =
new FileWriter("names.txt", true);
PrintWriter pw = new PrintWriter(fw);
PrintWriter outFile =
new PrintWriter("F:\PriceList.txt");
PrintWriter outFile = new
PrintWriter("C:\Users\kmihavic\Documents\NetBeansProjects\FileWriteDemo ames.txt");
File myFile = new File("Customers.txt");
Scanner inputFile = new Scanner(myFile);
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine(); //user can type vs. \
File file = new File(filename);
Scanner inputFile = new Scanner(file);
String str = inputFile.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
// Read until the end of the file.
while (inputFile.hasNext()) //returns false at eof
{
String str = inputFile.nextLine();
System.out.println(str);
}
inputFile.close();// close the file when done.
Explanation / Answer
int countWords, countCharacters, countCapitals;
StringTokenizer st, stc;
while (inputFile.hasNext()) //returns false at eof
{
String str = inputFile.nextLine();
countCharacters += str.length();
st = new StringTokenizer(str);
while(st.hasMoreTokens()) {
countWords++;
String word = st.nextToken();
char ch = word.charAt(0);
if (Character.toLowerCase(ch)!=ch)
countCapitals++;
}
System.out.println(str);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.