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