Using what you learned in the examples above, write your own class called, Parse
ID: 3600461 • Letter: U
Question
Using what you learned in the examples above, write your own class called, ParseForCaps that does the following 1. Prompts the user for the name of a plain text file 2. Creates a Scanner to read in each line of the file. 3. Creates a second Scanner to read each token (word) from the line. 4. Prints out the first letter of the token if it is uppercase Either write an if statement to check for upper case or use the method Character.isUpperCase(ch) to check if char variable ch is uppercase Sample Session The input file words.txt has the following contents: Amazing But we Can't Everything For Goodness sake The output of your program should look like this: Please enter a plain text File nae Capital first letters: ABCDEFGExplanation / Answer
ParseForCaps.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ParseForCaps {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a plain text file name: ");
String fileName = scan.next();
File file = new File(fileName);
if(file.exists()) {
Scanner readLine = new Scanner(file);
String capitalFirstLetters = "";
while(readLine.hasNextLine()) {
String line = readLine.nextLine();
Scanner readWords = new Scanner(line);
while(readWords.hasNext()) {
String word = readWords.next();
if(Character.isUpperCase(word.charAt(0))) {
capitalFirstLetters = capitalFirstLetters+word.charAt(0);
}
}
}
System.out.println("Capital first letters: "+capitalFirstLetters);
} else {
System.out.println("File does not exist");
}
}
}
Output:
Please enter a plain text file name:
D:\words.txt
Capital first letters: ABCDEFG
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.