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

Are there any errors or suggestions to improve this code: package workingfiles;

ID: 3834714 • Letter: A

Question

Are there any errors or suggestions to improve this code: package workingfiles; import java.io.FileReader; import java.io.IOException; import java.io.BufferedReader; public class WorkingFiles { //declare the counter variable public static int counter = 0; public static void main(String[] args) throws IOException { BufferedReader read = null; //try and catch block try { String myLine; read = new BufferedReader(new FileReader("Text.txt")); System.out.println("The text says: ") ; //read each word while ((myLine = read.readLine())!= null){ String[] words = myLine.split(" "); System.out.println(myLine); counter = counter + words.length; } System.out.println("The number of words read is " + counter); }catch(IOException e){ e.printStackTrace(); //close file }finally { try{ if(read != null)read.close(); }catch(IOException e){ e.printStackTrace(); } } } }

Explanation / Answer

Text.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\Text.txt)

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.

___________________

WorkingFiles.java

package workingfiles;

import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;

public class WorkingFiles {
   // declare the counter variable
   public static int counter = 0;

   public static void main(String[] args) throws IOException {
       BufferedReader read = null;
       // try and catch block
       try {
           String myLine;
           read = new BufferedReader(new FileReader("D:\aboutComputer.txt"));
           System.out.println("The text says: ");
           // read each word
           while ((myLine = read.readLine()) != null) {
               //parsing the string based on delimeter " "
               String[] words = myLine.split(" ");
              
               //Displaying the contents of the file
               System.out.println(myLine);
              
               //Finding the no of words in the file
               counter += words.length;
           }
          
           //Displaying the no of words in the file
           System.out.println("The number of words read is " + counter);
       } catch (IOException e) {
           e.printStackTrace();
           // close file
       } finally {
           read.close();
       }
   }
}

____________________

Output:

The text says:
A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.
The number of words read is 75

____________Thank You

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