Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Simple JAVA I/O exception question Please follow the bolded instruction o

ID: 3727830 • Letter: P

Question

Please Simple JAVA I/O exception question

Please follow the bolded instruction of # of lines required etc TY

/**

This program prints the number of characters, words,

and lines in several files. When it encounters a file that does not

exist, the program prints the total counts of lines, words and

characters in all processed files and exits.

It produces output similar to the wc command in unix.

You can assume that the files end with a newline character.

When you catch the exception for no file,

print a message as shown below (except generalize "horse.txt" to

filename).  

The following is the expected output for one example run of this method

(between the dashed lines):

-------------------

52 341 2158 cat.txt

9 89 501 dog.txt

7 69 388 mouse.txt

WordCountSeveral: horse.txt: No such file

68 499 3047 total

-------------------

*/

public static void readFiles(String[] files)

{

int totalLines = 0, totalWords = 0, totalCharacters = 0 ;

for (String filename : files) {

//-----------Start below here. To do: approximate lines of code = 7

// 1. try;

  

Scanner scanner = new Scanner(new File(filename)) ; //

int lines = 0, words = 0, characters = 0 ; //

while (scanner.hasNextLine()) { //

String line = scanner.nextLine() ; //

//2. update lines

  

//3. update characters

  

StringTokenizer st = new StringTokenizer(line) ; //

//4. update words

  

  

System.out.println(lines + " " + words + " " + characters + " " + filename) ; //

//5. update totals;

totalLines += lines ; //

totalWords += words ; //

totalCharacters += characters ; //

  

//5. catch clause

  

//6. print WordCountSeveral: filename: No such file

  

//7. stop the loop

  

  

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}

System.out.println(totalLines + " " + totalWords + " " + totalCharacters + " total") ; //

}

}

Explanation / Answer

  
public static void readFiles(String[] files)

{

int totalLines = 0, totalWords = 0, totalCharacters = 0 ;

for (String filename : files) {
try {
//-----------Start below here. To do: approximate lines of code = 7
// 1. try;
Scanner scanner = new Scanner(new File(filename)) ; //
int lines = 0, words = 0, characters = 0 ; //
while (scanner.hasNextLine()) { //
  
String line = scanner.nextLine() ; //
  
//2. update lines
lines=lines+1;
  
  
//3. update characters
characters=characters+line.length();
  
  
StringTokenizer st = new StringTokenizer(line) ; //
  
//4. update words
words=words+st.countTokens();
  
}
System.out.println(lines + " " + words + " " + characters + " " + filename) ; //
//5. update totals;
totalLines += lines ; //
totalWords += words ; //
totalCharacters += characters ; //
  
  
  
//5. catch clause
  
  
  
//6. print WordCountSeveral: filename: No such file
  
  
  
//7. stop the loop
  
  
  
  
  
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
} catch (FileNotFoundException ex) {
System.out.println("WordCountSeveral: "+filename+": No such file");
break;
}

}

System.out.println(totalLines + " " + totalWords + " " + totalCharacters + " total") ; //

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote