In: Computer Science
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
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...");
}
}
}