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++.
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...
Write a MIPS Assembly language program to request a file name from the user, open the...
Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console. This is what I have so far: .data    fileName:   .space 100    prompt1:   .asciiz "Enter the file name: "    prompt2:    .asciiz ""    prompt3:   .asciiz "\n"    buffer:    .space 4096 .text    main:        #        li $v0, 4        la $a0, prompt1       ...
Requirements1. Name the java class (and filename) Week3_StringLab .2. Print a welcoming message. 3. Ask user...
Requirements1. Name the java class (and filename) Week3_StringLab .2. Print a welcoming message. 3. Ask user to enter a string. Use the nextLine() input method, 4. Use that string to test out at least 6 String methods, from the String class. 5. Of those 6, you must use these 3 methods: .split(), .indexOf (int ch), .subString (int beginningIndex). 6. Use 3+ other methods that you wish to try. 7. Print out the string BEFORE using each method .8. Have the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT