- Code: Select all
import java.io.*;
import java.util.Scanner;
class OutFile
{
public static void main(String[] args) throws IOException
{
String fileName;
int n;
String data;
Scanner s = new Scanner(System.in);
System.out.println("How many prices do you have?");
n = s.nextInt();
s.nextLine();
System.out.println("What do you want to call you file of prices? ");
fileName = s.nextLine();
FileWriter fwriter = new FileWriter(fileName);
PrintWriter outFile = new PrintWriter(fwriter);
for(int i = 1; i <= n; i++)
{
System.out.println("Enter a price: ");
data = s.nextLine();
outFile.println(data);
}
outFile.close();
System.out.println("Data written to file.");
}
}


