Assignment #120 and Programs that Write to Files
Code
///Name: Ali Kurland
/// Period: 6
/// Program Name: Programs that Write to Files
/// File Name: Receipt.java
/// Date Finished: 4/15/2016
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Receipt {
public static void main(String[] args) {
PrintWriter fileOut;
Scanner keyboard = new Scanner(System.in);
try{
fileOut = new PrintWriter("receipt.txt");
} catch(IOException e) {
System.out.println("Sorry, I can't open the file 'receipt.txt' for editing.");
System.out.println("Maybe the file exists and is read-only?");
fileOut = null;
System.exit(1);
}
Double price=2.345;
System.out.print("How many gallons would you like to buy? ");
Double gallons = keyboard.nextDouble();
Double total = price * gallons;
fileOut.println( "RECEIPT:" );
fileOut.println( "" );
fileOut.println( "| Gallons: " + gallons );
fileOut.println( "| Price/gallon: $ " + price );
fileOut.println( "| Fuel total: $ " + total );
fileOut.close();
}
}
Picture of the output