CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2011
    Posts
    0

    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;
    }

    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help! Printwriter/Fwriter

    Please post your code in code tags and explain exactly what your problem is.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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
  •  





Click Here to Expand Forum to Full Width

Featured