|
-
July 23rd, 2011, 08:28 PM
#1
Help! Printwriter/Fwriter
My ultimate goal is to ping user for insurance company name, the quotes they received for each area, and then give them a total. I need to open a file, write to the file and then close the file, at the end I need to display all inputs. I can't seem to get past the creating of the file!
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;
/**
This program writes insurance quotes to file.
*/
public class AutoInsurance {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
String filename = "insurance.txt"; //File name
int insuranceco; //Insurance company name
int bodilyinjury=0; //quote for annual coverage for bodily injury
int propertydamage=0; //quote for annual coverage for property damage
int uninsureddrivers=0; //quote for annual coverage for uninsured drivers
int Total = bodilyinjury+propertydamage+uninsureddrivers;
//Create scanner object for keyboard input.
Scanner keyboard = new Scanner (System.in);
//Get Insurance information.
System.out.print("What is the name of the insurance company");
insuranceco = keyboard.nextInt();
System.out.print("what is the quote for the Bodily Injury coverage?");
bodilyinjury = keyboard.nextInt();
System.out.print("what is the quote for the Property Damage coverage?");
propertydamage = keyboard.nextInt();
System.out.print("What is the quote for the uninsured driver coverage?");
uninsureddrivers = keyboard.nextInt();
//Get the filename
System.out.print("Opening file name: "+filename);
filename = keyboard.nextLine();
//Open the file.
PrintWriter outputFile= newPrintWriter("insurance.txt");
//write file.
outputFile.println(insuranceco);
outputFile.println(bodilyinjury);
outputFile.println(propertydamage);
outputFile.println(uninsureddrivers);
outputFile.println(Total);
//close file
outputFile.close();
//Append File
FileWriter fwriter2= new FileWriter("insurance.txt", true);
PrintWriter outputFile2 = new PrintWriter (fwriter2);
//Get Insurance information.
System.out.print("What is the name of the insurance company");
insuranceco =keyboard.nextInt();
System.out.print("what is the quote for the Bodily Injury coverage?");
bodilyinjury=keyboard.nextInt();
System.out.print("what is the quote for the Property Damage coverage?");
propertydamage=keyboard.nextInt();
System.out.print("What is the quote for the uninsured driver coverage?");
uninsureddrivers=keyboard.nextInt();
//write file.
outputFile.println(insuranceco);
outputFile.println(bodilyinjury);
outputFile.println(propertydamage);
outputFile.println(uninsureddrivers);
outputFile.println(Total);
//close file
outputFile.close();
//Append File
FileWriter fwriter3= new FileWriter("insurance.txt", true);
PrintWriter outputFile3 = new PrintWriter (fwriter3);
//Get Insurance information.
System.out.print("What is the name of the insurance company");
insuranceco =keyboard.nextInt();
System.out.print("what is the quote for the Bodily Injury coverage?");
bodilyinjury=keyboard.nextInt();
System.out.print("what is the quote for the Property Damage coverage?");
propertydamage=keyboard.nextInt();
System.out.print("What is the quote for the uninsured driver coverage?");
uninsureddrivers=keyboard.nextInt();
//write file.
outputFile.println(insuranceco);
outputFile.println(bodilyinjury);
outputFile.println(propertydamage);
outputFile.println(uninsureddrivers);
outputFile.println(Total);
//close file
outputFile.close();
//Information double check below
//Artichoke Insurance Company
double BI=238.59;
double PD=365.87;
double UI=163.29;
double Totalled=BI=PD=UI;
//Tomato Insurance COmpany
String Insuranceco1 = "Tomato Insurance Company";
double BI2=276.95;
double PD2=343.78;
double UI2=182.92;
double Total2=BI2+PD2+UI2;
//Beans Insurance Company
String Insurnaceco2= "Beans Insurance Company";
double BI3=254.31;
double PD3=335.86;
double UI3=173.46;
double Total3=BI3+PD3+UI3;
//Beats Insurance Company
String Insuranceco3="Beats Insurance Company";
double BI4=307.28;
double PD4=319.54;
double UI4=157.92;
double Total4= BI4+PD4+UI4;
}
private static void println(int uninsureddrivers) {
// TODO Auto-generated method stub
}
private static PrintWriter newPrintWriter(String string) {
// TODO Auto-generated method stub
return null;
}
}
-
July 26th, 2011, 02:42 AM
#2
Re: Help! Printwriter/Fwriter
Please post your code in code tags and explain exactly what your problem is.
-
July 26th, 2011, 04:36 PM
#3
Re: Help! Printwriter/Fwriter
If you're having problems with file I/O, post up any error messages you get. I notice you aren't using exceptions, so you're not handling I/O errors. I would recommend you put in appropriate try...catch blocks to handle potential I/O exceptions and display the error messages.
It's hard enough to find an error in your code when you're looking for it; it's even harder when you've assumed your code is error-free...
S. McConnell
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|