Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Errors in this JAVA application, need help

    I need to submit this program in a few hours. I posted this problem in another forum but no help. I hope someone can help me with this. Here it is:

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class PatientDetails extends JFrame implements ActionListener
    {
    JFrame frame;
    JPanel panel;
    JLabel patientFirstName;
    JLabel patientMiddleName;
    JLabel patientLastName;
    JLabel patientPhoneNumber;
    JLabel patientPostalAddress;
    JTextField textPatientFirstName;
    JTextField textPatientMiddleName;
    JTextField textPatientLastName;
    JTextField textPatientPhoneNumber;
    JTextField textPatientPostalAddress;
    JComboBox patientChoice;
    JList paymentOption;
    JButton button;
    
    public static void main (String args[])
    {
     frame = new JFrame ("PatientDetails");
     panel = new JPanel ();
     frame.getContentPane().add(panel);
     panel.setLayout (new FlowLayout ());
     
     patientFirstName = new JLabel ("First Name: ");
     textPatientFirstName = new JTextField (15);
     
     patientMiddleName = new JLabel ("Middle Name: ");
     textPatientMiddleName = new JTextField (15);
     
     patientLastName = new JLabel ("Last Name: ");
     textPatientLastName = new JTextField (15);
     
     patientPhoneNumber = new JLabel ("Phone Number: ");
     textPatientPhoneNumber = new JTextField (15);
     
     patientPostalAddress = new JLabel ("Address: ");
     textPatientPostalAddress = new JTextField (50);
     
     String listOfPaymentOptions [] = {"Credit Card", "Cash", "Check"};
     paymentOption = new JList (listOfPaymentOptions);
     
     String memberChoice [] = {"New Patient", "Regular Patient", "Guest Patient"};
     patientChoice = new JComboBox (memberchoice);
     
     button = new JButton ("Submit");
     
     panel.add(patientFirstName);
     panel.add(textPatientFirstName);
     panel.add(patientMiddleName);
     panel.add(textPatientMiddleName);
     panel.add(patientLastName);
     panel.add(textPatientLastName);
     panel.add(patientPhoneNumber);
     panel.add(textPatientPhoneNumber);
     panel.add(patientPostalAddress);
     panel.add(textPatientPostalAddress);
     panel.add(paymentOption);
     panel.add(patientChoice);
     panel.add(button);
    }
    public class PatientDetails (ActionEvent addingInfo)
    {
     Object abc = addingInfo.getSource ();
     if (abc == button)
     {
      String entry = textPatientFirstName.getText () + ":" + textPatientMiddleName.getText () + ":" + textLastName.getText () + ":" + new String
      (textPatientPhoneNumber.getText()) + ":" + new String (textPatientPostalAddress.getText());
      try
      {
       RandomAccessFile patientFile = new RandomAccessFile ("C:\\Documents and Settings\\Ryan\\My Documents\\File.txt", "rw");
       patientFile.seek(patientFile.length());
       patientFile.writeBytes (entry);
      }
      catch (IOException error)
      {
       showStatus ("Cannot write to file" +error);
      }
     }
    }
    }
    If someone can please tell me what is wrong with this and how I can fix it. I'm only encountering 2 errors which I can't seem to fix.

    ryan

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perhaps a list of the errors would help us? Also, i don't think anyone is inclined to do your homework for you

  • #3
    New Coder
    Join Date
    Jul 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I wasn't asking anyone to do my homework, I was asking for some assistance since this is my very first java program i'm making by myself. doesn't matter now, I had to submit the program with errors in it. i remember one of the errors were 'cannot define non-static variable' or something like that. but yeah.

  • #4
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1. The first thing your class must have is a method (javaspeak for function) with the same name as your class. This is called the constructor. Every time an instance of your class object is created, the constructor is invoked. I'm pretty sure this is required. In your code, you have:
    Code:
    public class PatientDetails (ActionEvent addingInfo) {
    but when you create a constructor, you can not use the keyword class. This makes the java compiler think you want to create another class. That's why, in your code, you get an error saying the class is already defined in this scope. You need a method like this:
    Code:
    public PatientDetails() {
    }
    One more thing, this is usually the first method defined in a class. It is more asthetic to have it at the top of your code.

    2. Next, since you are implementing the ActionListener interface, you must define the void actionPerformed(ActionEvent x) method. I think that's what you were trying to do with
    Code:
    public class PatientDetails (ActionEvent addingInfo) {
    Try changing this to public void actionPerformed(ActionEvent addingInfo) {.

    3. Now the compiler is throwing errors for referencing variables in the static context. I don't think it would beseech you (or especially me) to try to explain the static context, so just move all of your code from void main(args) to PatientDetails() constructor, then in your main() method add this:
    Code:
    new PatientDetails();
    This will create a new, static instance of PatientDetails (thus invoking the constructor, see #1).

    4. memberchoice should be memberChoice. Remember nearly everything in java is case sensitive.

    5. In your new actionPerformed method you refer to textLastName, but it should be textPatientLastName.

    6. In your actionPerformed method you invoke showStatus, however there is no showStatus() method in your class. You can't do that.

    I corrected those things and didn't get any errors.

  • #5
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One other thing...
    I've found that things go much smoother if you "debug as you go". When I'm coding I will write a little piece of code, compile and run it, then write a little more, compile, write a little more, compile. This will let you work out the problems in your code WHILE you are coding it.

  • #6
    New Coder
    Join Date
    Jul 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey rynox, thanks for the help. I will correct it. The teacher in my class didn't specifically tell us to create a constructor after the class name. Also, would making the showStatus method a comment fix it or anything? just a thought .

    ryan

  • #7
    New Coder
    Join Date
    Jul 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Instead of showStauts, would I instead put "System.out.println"? I would think so, but then where would it be displayed.

  • #8
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To please the compiler I just removed the line that said showStatus(...

    System.out.println() would work, it will output it to the java console. The problem is that many java windows apps like the one you created don't even show the java console, so that wouldn't be a very effective error message.

    The best thing to do would be create a new class called AlertBox or something like that, then create a new instance when you want to send an alert message:

    new AlertBox("Error message!");

    Not sure if you discussed this in your class, but this is why object oriented programming is so important. You can create one class called AlertBox and use it over and over and never have to reprogram it.

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    you don't need the default constructor, if it is not present, the compiler will add it for you. It is good programming practice to add it, but not mandatory.
    I am the luckiest man in the world

  • #10
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Roelf. I learn something new every day.

  • #11
    New Coder
    Join Date
    Jul 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You guys are a great help. Just one more question, I want the details entered by a user to be stored in the file. the 'randomaccessfile' doesn't seem to be doing it for me so i'm not sure how to do it? Do I have to set policies or something, and if so, how? sorry for the many question but i'm just learning java and no i have no covered alert boxes yet.

    ryan


  •  

    Posting Permissions

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