Question

In: Computer Science

Java assignment Get a file name fname for output • Get number of data (numbers) (N)...

Java assignment

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

• Write the numbers into the file fname

Solutions

Expert Solution

The code is given below

/******************************************************************************

Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.io.FileWriter;
import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   try{
   System.out.println("Enter a number: ");
   Scanner in=new Scanner(System.in);
   int n=in.nextInt();
   int[] arr=new int[n];
   for(int i=0;i<n;i++){
   arr[i]=in.nextInt();
   }
   in.nextLine();//taking last unskipped enter
   System.out.pritnln("Enter fileName");
   String fname=in.nextLine();
FileWriter fw=new FileWriter(fname);
for(int i=0;i<n;i++){
fw.write(arr[i]+"\n");
}
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
   }
}


Related Solutions

Submit a java file. Write a program where you input 4 numbers and the output generates...
Submit a java file. Write a program where you input 4 numbers and the output generates the average of the 4 numbers. The output may not be negative and <= 100. If any value is negative or >100, it should be replaced by the value of 30. For all such values, you will replace them with a value of 10. The program should produce the following output: Today's average amount is: $xx.xx.
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are smaller than n and have a digit 7. For example, if n is 100, the program should output 7, 17, 37, 47, 67, 71, 73, 79, and 97.
File has a format Name and number, the number represents power. The name and the (integer)...
File has a format Name and number, the number represents power. The name and the (integer) power are separated by some amount of space. Importantly, any line that begins with a hash '#' are comments and they need to be ingored. Write a program that reads from that file, and prints out only the name of the hero with the strongest power. That name should be capitalized (not uppercase, but capitalized, as in 'Galadriel') Here is the heroes.txt # DC...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a...
1. Number of Kids Type all the numbers from the whiteboard into a data file n_kids.txt,...
1. Number of Kids Type all the numbers from the whiteboard into a data file n_kids.txt, one number per line. Write a program n_kids.py that reads the data from n_kids.txt, does some math, then displays the following: Total number of families: Total number of kids: Average number of kids per family: (to 2 dp) Maximum number of kids in a family: Minimum number of kids in a family: Write the above results to a different file (results.txt, not to the...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
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.
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
Module/Week 2 ASSIGNMENT (INPUT/OUTPUT) The number of permutations of a set of n items taken r...
Module/Week 2 ASSIGNMENT (INPUT/OUTPUT) The number of permutations of a set of n items taken r at a time is given by the following formulan!/r !(n- r )!: where n! is the factorial of n, r! is the factorial of r, and (n-r)! is the factorial of the result of n-r. The factorial of a number n can be solved using the following formula: n!=e-n nn √ 2πn. If there are 18 people in your class and you want to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT