Results 1 to 5 of 5
  1. #1
    mrajan's Avatar
    mrajan is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default whats is wrong with this app??

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package labassignment3;
    import java.util.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.awt.event.*;
    /**
    *
    * @author Mathew Rajan & Kristin Smith
    */
    public class LabAssignment3 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args)
    throws FileNotFoundException , IOException
    {
    // User information input
    String fNameIn;
    String lNameIn;
    String mNameIn;
    String hAddressIn;
    String EducationIn;
    String ageIn;
    String heightIn;
    String weightIn;

    // User information output
    String fNameOut;
    String lNameOut;
    String mNameOut;
    String hAddressOut;
    String EducationOut;
    int ageOut;
    int weightOut;
    int heightOut;

    //Declaring new person memory variables
    Person client;

    //Instantiating the variables and creates an object
    client = new Person ();

    //Following code creates or opens the file so that information can be stored
    PrintWriter personFile = new PrintWriter (new FileWriter (new File ("C:\\PersonInfoOut.dat"),true));

    //Creates GUI using the JOptionPane to ask for input
    fNameIn = JOptionPane.showInputDialog(null,
    "Enter Your First Name",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    lNameIn = JOptionPane.showInputDialog(null,
    "Enter Your Last Name",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    mNameIn = JOptionPane.showInputDialog(null,
    "Please enter your Middle name if any",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    ageIn = JOptionPane.showInputDialog(null,
    "Please enter your age",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    hAddressIn = JOptionPane.showInputDialog(null,
    "Please enter your Home address(street address)",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    hAddressIn = JOptionPane.showInputDialog(null,
    "Please enter your Home address(City, State)",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    hAddressIn = JOptionPane.showInputDialog(null,
    "Please enter your Home address(Zipcode)",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    heightIn = JOptionPane.showInputDialog(null,
    "Please enter your Height in cm(1in= 2.54cm so 1ft= 30.48cm)",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    weightIn = JOptionPane.showInputDialog(null,
    "Please enter your weight in kilograms(1lb= 0.45359237kg)",
    "Personal Data Creater",
    JOptionPane.QUESTION_MESSAGE);

    EducationIn = JOptionPane.showInputDialog(null,
    "Please enter your highest Level of Education",
    "Job Application",
    JOptionPane.QUESTION_MESSAGE);


    //Convert the numeric string values into an integer data Using
    //the Static Class Integer and assign the values to the person object
    client.fName = fNameIn;
    client.lName = lNameIn;
    client.mName = mNameIn;
    client.hAddress = hAddressIn;
    client.Education = EducationIn;
    client.age = Integer.parseInt (ageIn.trim());
    client.height= Integer.parseInt (heightIn.trim());
    client.weight= Integer.parseInt (weightIn.trim());

    //Assign the values to output data using dot notation
    fNameOut = client.fName;
    lNameOut = client.lName;
    mNameOut = client.mName;
    hAddressOut = client.hAddress;
    EducationOut = client.Education;
    ageOut = client.age;
    heightOut= client.height;
    weightOut= client.weight;

    // Store the data into the personFile
    personFile.append(fNameOut + " " + lNameOut + " " + mNameOut + " "
    + ageOut + " "+ hAddressOut+" " + heightOut + " " + weightOut + " "+ EducationOut+"END\n ");
    personFile.append ("These are the client's details \n");
    personFile.close();

    // Show the user the data through JOptionPane
    JOptionPane.showMessageDialog(null, "You Entered the Following \n"
    + "Your first name is " + fNameOut + "\n"
    + "Your last name is " + lNameOut + "\n"
    + "Your middle name is " + mNameOut + "\n"
    + "Your age is " + ageOut + "\n"
    + "Your home address is " + hAddressOut + "\n"
    + "Your height is " + heightOut + "cm\n"
    + "Your weight is " + weightOut + "kg"
    + "your highest education level is " + EducationOut + "\n");


    }

    }

    SHOWS NO ERROR :)

    PERSON CLASS

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package labassignment3;

    /**
    *
    * @author Mathew Rajan & Kristin Smith
    */
    public class Person {
    String fName;
    String lName;
    String mName;
    String hAddress;
    String Education;
    int age;
    int height;
    int weight;

    }


    NO ERRORS :D

    BUT WHEN I RUN IT I GET THIS:

    run:
    Exception in thread "main" java.io.FileNotFoundException: C:\PersonInfoOut.dat (Access is denied)
    at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.j ava:177)
    at java.io.FileWriter.<init>(FileWriter.java:90)
    at labassignment3.LabAssignment3.main(LabAssignment3. java:50)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds) :confused:

    WHY IS THIS HAPPENING, PLEASE HELP ME:(

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    when you call new FileWriter(file, true), you're asking to open an existing file and append to it. If the file doesn't exist, thats no good.

    two options:
    1. use new FileWriter(file). this will replace the file every time, whether or not it exists. It will overwrite the previous execution data.

    2. if you have to keep the previous data and want indeed to append to the file, do this:

    File theFile = new File(...);
    if (!theFile.exists()) {
    theFile.createNewFile();
    }

    new FileWriter(theFile, true);

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Your program cannot access C:\PersonInfoOut.dat
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    The code for PrintWriter works as shown.
    Here's what I get with the code the second time I run it after changing the properties on the file created the first run to be ReadOnly:
    java.io.FileNotFoundException: D:\PersonInfoOut.dat (Access is denied)
    at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileWriter.<init>(Unknown Source)
    at TestArrayDef.main(TestArrayDef.java:9)

  5. #5
    mrajan's Avatar
    mrajan is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Whats wrong with this code???
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 05:46 PM
  2. Database help... whats wrong?
    By neosnokia in forum JDBC
    Replies: 4
    Last Post: 06-09-2009, 11:17 PM
  3. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 03:51 AM
  4. Whats wrong with my maths???
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 11-03-2008, 05:52 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •