Question

In: Computer Science

Write a java code to demonstrate the File IO. Your code should get the following information...

Write a java code to demonstrate the File IO. Your code should get the following information from the user.

• Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user

• Get N numbers from the users through keyboard and store them in an array

• Get M (How many numbers to read from file)

• (Or)You are free to use same N for M (use N for both purposes) You have to define two methods:

1) To write the numbers from the array into the file fname

2) To read numbers from the file fname, store them in an array, and compute average of the numbers.

The method should display the numbers from the array and the average value.

Solutions

Expert Solution

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileProcessing {
   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter filename: ");
       String fname = sc.next();
       //reading n
       System.out.println("Enter numer of elements ");
       int n = sc.nextInt();
       int arr[] = new int[n];
       //reading numbers into array
       System.out.println("Enter " + n + " elements : ");
       for (int i = 0; i < n; i++)
           arr[i] = sc.nextInt();
       //calling method to write into file
       writeIntoFile(arr, fname);
       //calling method to read from file
       readNumbersAndDisplay(fname, n);
   }
//reading from file
   private static void readNumbersAndDisplay(String name, int aN) throws Exception {
       Scanner sc = new Scanner(new File(name));
       int i = 0;
       int arr[] = new int[aN];
       double sum = 0;
       while (sc.hasNext()) {
           arr[i] = sc.nextInt();
           sum += arr[i++];
       }
       double avg = sum / aN;
       System.out.println("Sum : " + sum);
       System.out.println("Average: " + avg);
   }

   private static void writeIntoFile(int[] arr, String name) throws Exception {
       PrintWriter pw = new PrintWriter(new File(name));
       for (int i : arr)
           pw.println(i);
       pw.close();
   }
}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Using Java, define a train class and write code to demonstrate its functionality The train should...
Using Java, define a train class and write code to demonstrate its functionality The train should run between Boston and New York, stopping at different, different station along the way. Provide output to indicate the train’s current status.
How to write IO program in java?
How to write IO program in java?
File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
Name your source code file sh.c Write your own simple shell. Your shell should prompt the...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the user for a command, run it, then prompt the user for their next command. If the user types "exit", then the shell should terminate. The shell should ignore Ctrl-C. Inside the attached parse.h header file, there is a parse function that you can use to split up the command line into separate strings. Recall that execvp accepts two arguments: the name of the command,...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
The purpose of this homework is to test your knowledge of File IO and Collections and...
The purpose of this homework is to test your knowledge of File IO and Collections and the language would be java. Suppose that we have a file Employee.csv which contains employee information of a certain company. The first line of the file contains the following header: employeeId,firstName,lastName,dob,gender,position After the header line, each employee information is written on a separate line which consists of 6 comma-separated fields, for example:1234567, John, Smith,20/12/1989, M, Software Engineer Question 1. Write code to read the...
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of...
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of Eclipse showing output of program. (JPG or PNG format only) Write a program to play the game of Stud Black Jack. This is like regular Black Jack except each player gets 2 cards only and cannot draw additional cards. Create an array of 52 integers. Initialize the array with the values 0-51 (each value representing a card in a deck of cards) in your...
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
Why is java programming better than bash scripting. Why should programmers write in java code ?...
Why is java programming better than bash scripting. Why should programmers write in java code ? Come up with situations where java is a better option.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT