Question

In: Computer Science

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.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


// fileDataNums.txt

23
34
45
56
67
76
65
54
43
32
21
78
89
90
9
8
76
44
55
67
77
88

_________________________

// ReadFilenos.java

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

public class ReadFilenos {

   public static void main(String[] args) throws FileNotFoundException {

       int m,n=0;
       double sum=0;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter m:");
       m=sc.nextInt();
      
      
       Scanner fileOpen = new Scanner(new File("fileDataNums.txt"));
while(fileOpen.hasNext())
{
   fileOpen.nextDouble();
   n++;
}
  
fileOpen.close();
  
if(m<=n)
{
   double nos[]=new double[m];
   fileOpen=new Scanner(new File("fileDataNums.txt"));
   for(int i=0;i<m;i++)
   {
       nos[i]=fileOpen.nextDouble();
       sum+=nos[i];
   }
   fileOpen.close();
   System.out.println("Displaying the nos :");
   for(int i=0;i<m;i++)
   {
       System.out.print(nos[i]+" ");
   }
   System.out.println("\nAverage :"+(sum/m));
}
else
{
   System.out.println("** Invalid input **");
}
   }

}
___________________________

Output:

Enter m:10
Displaying the nos :
23.0 34.0 45.0 56.0 67.0 76.0 65.0 54.0 43.0 32.0
Average :49.5

_______________Could you plz rate me well.Thank You


Related Solutions

Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
Script 3: Ask the user for a file's name If the file exists, ask them if...
Script 3: Ask the user for a file's name If the file exists, ask them if they would like to (C)opy, (M)ove, or (D)elete it by choosing C, M, or D If the user chooses C, ask for the destination directory and move it there If the user chooses M, ask for the destination directory and move it there If the user chooses D, delete the file. Ensure that the user enters only C, M, or D, warning them about...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask the user for a line of text that the system should save to the file Add to the file the line of text entered by the user Ask the user for a second line of text that the system should save to the file Append at the end of the file the line of text entered by the user Create a copy of the...
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
Using the given file, ask the user for a name, phone number, and email. Display the...
Using the given file, ask the user for a name, phone number, and email. Display the required information. These are the Files that I made: import java.util.Scanner; public class Demo5 { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("New number creation tool"); System.out.println("Enter name"); String name = keyboard.nextLine(); System.out.println("Enter phone number"); String phoneNumber = keyboard.nextLine(); System.out.println("Enter email"); String email = keyboard.nextLine(); Phone test1 = new SmartPhone(name, phoneNumber, email); System.out.print(test1); System.out.println("Telephone neighbor: " + ((SmartPhone) test1).getTeleponeNeighbor()); }...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming read and make a while loop that A) Reads in numbers B) Sums them up. C) Counts how many there are. D) Prints "With 10 numbers, the average is blank.." every 10 numbers. So with every 10 numbers read in print the average. 3. After the loop calculate average of all numbers and print it. So for example once the file is open the...
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT