Results 1 to 2 of 2
  1. #1
    CloseQuarters is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default Please Help new to Java and keep getting error

    Ok so im new to Java and i keep getting this error everytime i try to run my script. The error is:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at patient.patient_inform.add_patient(patient.java:98 )
    at patient.patient.main(patient.java:26)

    The error occurs at different points depending on what data i enter when i run my code.

    here is my code Please Help :(



    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.StringTokenizer;

    import javax.swing.JOptionPane;
    public class patient

    {

    public static void main(String[] args)
    {
    int option;
    String value;
    patient_inform patient = new patient_inform ();
    patient.start_system();
    value = patient.menu();
    option = Integer.parseInt(value);
    while(option!=4)
    {
    if(option==1)
    {
    patient.add_patient();
    }
    else if (option==2)
    {
    patient.modify_patient();

    }
    else if (option==3)
    {
    patient.patient_report();
    value = patient.menu();
    option =Integer.parseInt(value);
    }
    }
    //while loop
    patient.exit_system();
    System.exit(0);
    }
    //main method
    }
    class patient_inform
    {
    int count = -1, i;
    String lasname[] = new String[i];
    String firname[] = new String[i];
    int age[] = new int[i];
    int rstat[] = new int[i];
    String romnum[] = new String[i];
    String codes[] = new String[i];
    String gendr[] = new String[i];

    void start_system()
    {
    String newLine;
    try
    {
    BufferedReader Patient_file = new BufferedReader(new FileReader("patient.dat"));
    while ((newLine = Patient_file.readLine()) != null)
    {
    StringTokenizer delimiter = new StringTokenizer(newLine, "#");
    count=count+1;
    lasname[count] = delimiter.nextToken();
    firname[count] = delimiter.nextToken(); rstat[count] = Integer.parseInt(delimiter.nextToken());
    romnum[count] = delimiter.nextToken();
    gendr[count] = delimiter.nextToken();
    age[count] = Integer.parseInt(delimiter.nextToken());
    }
    Patient_file.close();
    }
    catch (IOException error)
    {
    System.out.println("Error on file rad"+ error);
    }
    }

    String menu()
    {
    String rnum;
    String output = "Delaware Medical Hospital" + "\n" + "\n" +
    "1. Add/Modify the Patient Data" + "\n" +
    "2. Add/Modify the Surgical Data" + "\n" +
    "3. Report the Section" + "\n" +
    "4. Exit the System" + "\n" + "\n" +
    "Make selection";
    rnum = JOptionPane.showInputDialog(null, output, "", JOptionPane.QUESTION_MESSAGE);;
    return rnum;
    }
    void add_patient()
    {
    String svalue,Output;
    count=count+1;
    Output = "Enter patients last name";
    lasname[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    Output = "Enter patients first name";
    firname[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    Output = "Enter patients Gender (m for Male or f for Female only!";
    gendr[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    Output = "Enter patients age";
    svalue = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    age[count] = Integer.parseInt(svalue);
    Output = "Enter patients surgical status, 1 for surgical patient and 2 for non-surgical patient";
    svalue = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    rstat[count] = Integer.parseInt(svalue);
    Output = "Enter patients room status, I for in-patient, O for out-patient";
    romnum[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    }
    void modify_patient()
    {
    int poption;
    String modnum;
    String output = "Modify Patient Menu" + "\n" + "\n" +
    "1. Add the Patient" + "\n" +
    "2. Delete the Patient" + "\n" +
    "3. Modify the Patient" + "\n" +
    "4. Leave the Patient Menu" + "\n" + "\n" +
    "Make selection";
    modnum = JOptionPane.showInputDialog(null, output, "", JOptionPane.QUESTION_MESSAGE);
    poption = Integer.parseInt(modnum);
    if (poption ==1)
    {
    String rvalu,Output;
    count=count+1;
    Output = "Enter patients last name";
    lasname[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    Output = "Enter patients first name";
    firname[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    Output = "Enter patients Gender (m for Male or f for Female only!";
    gendr[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    Output = "Enter patients age";
    rvalu = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    age[count] = Integer.parseInt(rvalu);
    Output = "Etner patients surgical status, 1 for surgical patient and 2 for non-surgical patient";
    rvalu = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    rstat[count] = Integer.parseInt(rvalu);
    Output = "Enter patients room status, I for in-patient, O for out-patient";
    romnum[count] = JOptionPane.showInputDialog(null, Output, "", JOptionPane.QUESTION_MESSAGE);
    }

    }

    void patient_report()
    {
    System.out.println("All Patient Information");
    for (i=0; i<=count; ++i);
    {
    System.out.println(lasname[i]+ ""+firname[i]+""+gendr[i]+""+age[i]+""+rstat[i]+""+romnum[i]);
    }
    }
    void exit_system()
    {
    try {
    BufferedWriter Patientfile = new BufferedWriter(new FileWriter("patient.dat"));;
    for(i=0; i<=count; ++i);;
    {
    Patientfile.write(lasname[i]+ "#"+firname[i]+"#"+gendr[i]+"#"+age[i]+"#"+rstat[i]+"#"+romnum[i]);
    Patientfile.newLine();
    }
    Patientfile.close();
    }
    catch (IOException error)
    {
    System.out.println("Eror on fileds write"+ error);
    }
    }
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    Not sure, but is variable "i" initialized in the patient_inform class?
    Java Code:
    class patient_inform
    {
    int count = -1, [COLOR="red"]i[/COLOR]; [COLOR="Red"]//< - not initialized[/COLOR] 
    String lasname[] = new String[[COLOR="red"]i[/COLOR]]; [COLOR="red"]//<- i being used[/COLOR]
    .
    .
    .
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. error with import java.io.*
    By osval in forum New To Java
    Replies: 5
    Last Post: 05-13-2010, 06:03 PM
  2. Java Hotspot Error
    By prashantredhat in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2008, 08:38 AM
  3. How to get error codes using java program
    By kasipandian in forum Web Frameworks
    Replies: 10
    Last Post: 05-25-2008, 06:00 PM
  4. java error message
    By baileyr in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 04:47 AM
  5. error in Java files
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 01:14 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
  •