我有一个名为“Examroom”的对象数组,其中包含字符串(名称)参数和 int(等待时间)参数。
客户端应该输入一个名称,然后程序可以将其添加到新数组元素中的对象中(CheckIn 方法)。
客户端还可以输入名称,程序会迭代数组,将用户输入的大写字母与数组元素对象中存储的名称的大写字母进行比较(CheckOut 方法)。
在 CheckIn 中,我能够很好地检索元素的内容,但是在 CheckOut 中,我无法使用循环调用数组 (ExamRoom[i].name == n.toUpperCase) 并获取空指针异常。
数组的大小为 4(4 个 ExamRoom),有时如果没有人来填充元素,则元素需要为空。我需要使用数组,不能使用像 arrayList 这样灵活的东西。请帮忙。
最初,CheckIn 不起作用,因为我使用了带有 i 和“ExamRoom[i].name”/ ExamRoom[i].waitTime 的 for 语句。我最终将 i 替换为计数器 (countNames),并在每次方法运行后简单地递增 countNames 的 for 循环。这有效,但我不确定为什么,或者我如何做类似 CheckOut 的事情 - 认为它需要它所具有的 for 循环。
ExamRoom 是 Patient 对象的数组 病人:
public class Patient
{
public String name;
public int waitTime;
Patient (String name, int waitTime)
{
this.name = name;
this.waitTime = waitTime;
}
}
从(主)菜单类调用方法:
final int NumRm = 4;
int WaitTime = -1;
Patient[] ExamRoom = new Patient[NumRm];
Queue<Patient> WaitingRoom = new LinkedList<Patient>();
switch (choice)
{
case -1:
break;
case 1:
System.out.println("Check in:");
System.out.println("---------");
System.out.println("Enter the name of the patient you would like to check in.");
String InPatientName = kbReader.nextLine();
Doctors_Office.CheckIn(InPatientName, WaitTime, ExamRoom, WaitingRoom);
break;
case 2:
System.out.println("Check out:");
System.out.println("----------");
System.out.println("Enter the name of the person you would like to check out.");
String OutPatientName = kbReader.nextLine();
Doctors_Office.CheckOut(OutPatientName, WaitTime, ExamRoom, WaitingRoom);
break;
The methods CheckIn and CheckOut, located in Doctors_Office Class - i dont think the contructor is doing anything becuase the methods themselves have what they need
public class Doctors_Office
{
static int i,j,k,l;
static int EmptyExmRoom;
static Boolean ExamRmFull;
static Boolean ExamRmEmpty;
static Boolean WaitingRmEmpty;
static int countNames = 0;
static int comb = 5;
int waitTime;
String name;
public Doctors_Office(String name, int waitTime, Patient[] ExamRoom, Queue<Patient> WaitingRoom)
{
this.waitTime = waitTime;
this.name = name;
}
public static void CheckIn (String n, int wt, Patient[] ExamRoom, Queue<Patient> WaitingRoom)
{
if (countNames < ExamRoom.length)
{
int RmNmbr = countNames + 1;
wt = 0;
ExamRoom[countNames] = new Patient(n.toUpperCase(), wt);//Enter Patient into the empty exam room.
countNames += 1;
System.out.println("The Patient was checked into Exam Room number " + RmNmbr + ".");
System.out.println(ExamRoom[countNames - 1].name + " bloop " + ExamRoom[countNames - 1].waitTime);
}
else if (countNames >= ExamRoom.length)//Exam Rooms are full
{
wt = comb;
WaitingRoom.add(new Patient(n.toUpperCase(),wt));//Enter Patient into the waiting room.
System.out.println("The Exam Rooms are currently full. Patient was sent to the Waiting Room.");
System.out.println(WaitingRoom.peek().name + " has a wait time of " + WaitingRoom.peek().waitTime + " minutes.");
countNames += 1;
comb += 5;
}
}
public static void CheckOut (String n, int wt, Patient[] ExamRoom, Queue<Patient> WaitingRoom)
{
int PatientExmRm;
int index;
int position;
//if (Doctors_Office.ExamRmEmpty == true)
if (countNames == 0)
{
System.out.println("There are not any Patients in the Exam Room to check out.");
}
//else if (Doctors_Office.ExamRmEmpty == false)// the Exam Room has patients.
else if (countNames != 0) // the Exam Room has patients.
{
// String pName = ExamRoom[countNames - 1].name;
// System.out.println(pName);
try {
for(i = 0; i < ExamRoom.length; i++)
{
System.out.println("bloop1");
if (ExamRoom[i].name == n.toUpperCase())// check if the target patient is in the exam room.
//if (ExamRoom[i].getName().equalsIgnoreCase(n) )
{
System.out.println("bloop2");
PatientExmRm = i +1;// Exam Room number that the patient is checking out of.
index = i;
Patient[] Copy = new Patient[ExamRoom.length - 1];
for (j = 0, k = 0; j < ExamRoom.length; j++)
{
System.out.println("bloop3");
if (j == index)
{
System.out.println("bloop4");
continue;
}
Copy[k++] = ExamRoom[j];
ExamRoom = Copy;
countNames -= 1;
}
System.out.println("The Patient " + n + " was sucessfully checked out of Exam Room " + PatientExmRm + ".");
if (WaitingRoom.isEmpty() == true)
{
System.out.println("There are not any Patients in the Waiting Room at this time");
}
else if (WaitingRoom.isEmpty() == false)
{
position = index;
String QueuedPatient = WaitingRoom.peek().name;
Patient[] New = new Patient[ExamRoom.length + 1];
for (l = 0; l < ExamRoom.length + 1; l++)
{
if (l < position - 1)
{
New[l] = ExamRoom[l];
}
else if (l == position - 1)
{
New[l] = WaitingRoom.peek();
New[l] = WaitingRoom.remove();
New[l] = Patient(WaitingRoom.peek().name, WaitingRoom.peek().waitTime);
}
else
{
New[l] = ExamRoom[l - 1];
}
}
System.out.println("The Patient " + QueuedPatient + " was moved from the Waiting Room to Exam Room number " + PatientExmRm + ".");
}
break;
}
else
{
System.out.println("The patient you are looking for is not in an Exam Room.");
}
}
}catch (NullPointerException e)
{
System.out.println(e);
}
}
}
any attempts to retrieve values from the array elements in a for loop yield an exception
I added in a try/catch, which stopped the program from quitting, but did not resolve the issue.
Thank you.
你似乎有复合问题
ExamRoom
(和WaitingRoom
)在菜单的每个循环上都已初始化...
final int NumRm = 4;
int WaitTime = -1;
Patient[] ExamRoom = new Patient[NumRm];
Queue<Patient> WaitingRoom = new LinkedList<Patient>();
switch (choice)
{
因此之前存储的所有信息都会丢失。
ExamRoom[i].name == n.toUpperCase()
不应该如何比较 String
。使用 String#equals
或 String#equalsIgnoreCase
代替(即 n.equalsIgnoreCase(ExamRoom[i].name)
)
您的
Doctors_Office
的用途和您的使用方式之间似乎存在脱节。
我的意思是,构造函数要求
waitTime
和 examRoom
属性,但您忽略了这些属性,并且似乎是通过 (static
) 方法传递它们。
public Doctors_Office(String name, int waitTime, Patient[] examRoom) {
this.waitTime = waitTime;
this.name = name;
}
我建议消除对
static
的依赖,并开始更多地使用 Doctors_Office
类(以及一般的 OO 原则)
例如...
public class DoctorsOffice {
private int countNames = 0;
private int comb = 5;
private int waitTime;
private String name;
private Patient[] examRoom;
public DoctorsOffice(String name, int waitTime, Patient[] examRoom) {
this.waitTime = waitTime;
this.name = name;
this.examRoom = examRoom;
}
public void checkIn(String n) {
if (countNames < examRoom.length) {
int RmNmbr = countNames + 1;
examRoom[countNames] = new Patient(n.toUpperCase(), 0);//Enter Patient into the empty exam room.
countNames += 1;
System.out.println("The Patient was checked into Exam Room number " + RmNmbr + ".");
System.out.println(examRoom[countNames - 1].name + " bloop " + examRoom[countNames - 1].waitTime);
} else if (countNames >= examRoom.length)//Exam Rooms are full
{
//wt = comb;
// You have a wait time property, make use of it instead
//WaitingRoom.add(new Patient(n.toUpperCase(), wt));//Enter Patient into the waiting room.
System.out.println("The Exam Rooms are currently full. Patient was sent to the Waiting Room.");
//System.out.println(WaitingRoom.peek().name + " has a wait time of " + WaitingRoom.peek().waitTime + " minutes.");
countNames += 1;
comb += 5;
}
}
public void checkOut(String n) {
int PatientExmRm;
int index;
int position;
//if (Doctors_Office.ExamRmEmpty == true)
if (countNames == 0) {
System.out.println("There are not any Patients in the Exam Room to check out.");
} //else if (Doctors_Office.ExamRmEmpty == false)// the Exam Room has patients.
else if (countNames != 0) // the Exam Room has patients.
{
//String pName = ExamRoom[countNames - 1].name;
//System.out.println(pName);
try {
for (int i = 0; i < examRoom.length; i++) {
System.out.println("bloop1");
if (n.equalsIgnoreCase(examRoom[i].name))// check if the target patient is in the exam room.
//if (ExamRoom[i].getName().equalsIgnoreCase(n) )
{
System.out.println("bloop2");
PatientExmRm = i + 1;// Exam Room number that the patient is checking out of.
index = i;
Patient[] Copy = new Patient[examRoom.length - 1];
for (int j = 0, k = 0; j < examRoom.length; j++) {
System.out.println("bloop3");
if (j == index) {
System.out.println("bloop4");
continue;
}
Copy[k++] = examRoom[j];
examRoom = Copy;
countNames -= 1;
}
System.out.println("The Patient " + n + " was sucessfully checked out of Exam Room " + PatientExmRm + ".");
//if (WaitingRoom.isEmpty() == true) {
// System.out.println("There are not any Patients in the Waiting Room at this time");
//} else if (WaitingRoom.isEmpty() == false) {
// position = index;
// String QueuedPatient = WaitingRoom.peek().name;
// Patient[] New = new Patient[ExamRoom.length + 1];
// for (l = 0; l < ExamRoom.length + 1; l++) {
// if (l < position - 1) {
// New[l] = ExamRoom[l];
// } else if (l == position - 1) {
// New[l] = WaitingRoom.peek();
// New[l] = WaitingRoom.remove();
// New[l] = Patient(WaitingRoom.peek().name, WaitingRoom.peek().waitTime);
// } else {
// New[l] = ExamRoom[l - 1];
// }
// }
// System.out.println("The Patient " + QueuedPatient + " was moved from the Waiting Room to Exam Room number " + PatientExmRm + ".");
//}
break;
} else {
System.out.println("The patient you are looking for is not in an Exam Room.");
}
}
} catch (NullPointerException e) {
System.out.println(e);
}
}
}
}
并使用...进行简单测试
DoctorsOffice office = new DoctorsOffice("Jane", 0, new Patient[5]);
office.checkIn("Bob");
office.checkOut("Bob");
表明核心概念有效 - 请记住,不要在菜单循环中创建
DoctorsOffice
的新实例!
请花时间学习和使用 Java 编程语言的代码约定
它会让你的代码更容易阅读/理解,也让你更容易阅读/理解其他人的代码