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

Java Reading and writing text files Write a java class ReadNumbers.java The file

ID: 3567655 • Letter: J

Question

Java Reading and writing text files

Write a java class ReadNumbers.java

The file will read a text file numbers.txt.

Each line of numbers.txt contains an integer number.

The first line of numbers.txt will contain how many numbers it will contain.

After first line there will be integers at each line. -1 will indicate the end of file.

Example:

10

1

20

37

3

4

23

90

11

13

19

-1

In the above file the first line contains a 10. So there will be 10 lines after the first line. Each of the 10 lines will contain an integer. At the end when -1 is read it is considered as end of file and there is no more input to read.

The program will initialize an array with size equal to the first integer in the file. In our above example it will initialize the array with size 10.

Then it will read each integer after the first line and place it in the array. The array for our above example will be like:

1

20

37

3

4

23

90

11

13

19

The program will then determine the maximum, minimum, average and sum for the integers in the array.

The program then creates another output text file as output.txt

Then it writes the maximum, minimum, average and total into the output.txt file

The format of the output.txt file for our above example will be like:

Maximum: 90

Minimum: 1

Total: 221

Average: 22

Use proper exception handling for files.

1

20

37

3

4

23

90

11

13

19

Explanation / Answer

//Read a text file consists of integer numbers

//Finds maxium,minimum,total and average of the numbes

//and writese to output.txt fie

//ReadTextFile.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReadFile
{
   public static void main(String[] args)
   {

       //Declaration of scanner object
       Scanner fileReader=null;
       int size;
       //Try catch block to handle file not found exception
       //while opening a text file "Sample.txt"
       try
       {  
           //Open file to read text from file
           fileReader=new Scanner(
                   new FileInputStream("numbers.txt"));          
       }
       catch (FileNotFoundException e)
       {
           //Exits the program if file not found.
           System.out.println("File not found");
           System.exit(0);
       }


       //read first integer that is size of the integer array
       size=fileReader.nextInt();

       //create an integer array of size 10
       int[] integerArray=new int[size];

       int index=0;
       //read integer from the file
       while(fileReader.hasNextInt())
       {

           int element=fileReader.nextInt();
           if(element!=-1)
               integerArray[index]=element;
           index++;
       }
       //close the file input stream.
       fileReader.close();
       System.out.println("Numbers in Array");
       printArray(integerArray);
      
      
      
      
       //Declare a PrintWriter class object
       PrintWriter fileWriter=null;
       //try catch block to handle output.txt file not found exception
       //when creating a file.
       try {
      
           //instantiate the fileWriter object with text file "output.txt"
           fileWriter=new PrintWriter(new FileWriter("output.txt"));
          
           //write maximum ,minimum,total and averaage
           fileWriter.write("Maximum : ");
           fileWriter.write(String.valueOf(getMax(integerArray)));
           fileWriter.write(" ");
           fileWriter.write("Minimum : ");
           fileWriter.write(String.valueOf(getMin(integerArray)));
           fileWriter.write(" ");
           fileWriter.write("Total : ");
           fileWriter.write(String.valueOf(getTotal(integerArray)));
           fileWriter.write(" ");
           fileWriter.write("Average : ");
           fileWriter.write(String.valueOf(getTotal(integerArray)/size));
          
       }
       catch (Exception e) {
          
           System.out.println(e);
       }
      
       //close fileWriter stream
       fileWriter.close();
      

   }//end of the main method


   //The method prints the elements in the array
   public static void printArray(int[] array)
   {

       for (int i = 0; i < array.length; i++)
           System.out.println(array[i]+" ");
   }


   //The method accept an integer array and returns the maximum element in the array
   public static int getMax(int[] array)
   {
       //Assume first element is maximum
       int max=array[0];
       for (int i = 1; i < array.length; i++)
           if(array[i]>max)
               max=array[i];
       return max;
   }

   //The method accept an integer array and returns the minimum element in the array
   public static int getMin(int[] array)
   {

       //Assume first element is minimum
       int min=array[0];
       for (int i = 1; i < array.length; i++)
           if(array[i]<min)
               min=array[i];
       return min;
   }


   //The method accept an integer array and returns the sum of the integers
   //in the array
   public static int getTotal(int[] array)
   {

       //To store the sum of the array values
       int total=0;
       for (int i = 0; i < array.length; i++)
               total+=array[i];
       return total;
   }

}//end of the class

------------------------------------------------------------------------------------------
Input text file
numbers.txt
10
1
20
37
3
4
23
90
11
13
19
-1

Sample output:

Numbers in Array
1
20
37
3
4
23
90
11
13
19

output.txt
Maximum : 90
Minimum : 1
Total : 221
Average : 22

Hope this would be helpful to 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