I am searching for records that have the start date: [userinput], if it has any matches, that record will be displayed, the issue is that the nature of my program it is neccesary for ALL matches (there can be multiple on the same day) need to be somehow displayed.
This is what I have so far:
public void searchDay() {
String idInputString = JOptionPane.showInputDialog(null, "Please enter the Date you're searching for:");
try {
for (int i = 0; i < orderID.length; i++) {
if (idInputString.equals(startDate[i])) {
txtOrderID.setText(orderID[i]);
txtOrderForename.setText(customerForename[i]);
txtOrderSurname.setText(customerSurname[i]);
txtOrderAddress1.setText(address1[i]);
txtOrderAddress2.setText(address2[i]);
txtOrderTown.setText(town[i]);
txtOrderCounty.setText(county[i]);
txtOrderPost.setText(postCode[i]);
txtOrderCarModel.setText(carModel[i]);
txtOrderCarReg.setText(carReg[i]);
txtOrderStartDate.setText(startDate[i]);
txtOrderStartTime.setText(startTime[i]);
txtOrderSerial.setText(serialNum[i]);
} else {
JOptionPane.showMessageDialog(null, "Order ID Doesn't Exist", "Error!", JOptionPane.WARNING_MESSAGE);
break;
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Order ID Doesn't Exist", "Error!", JOptionPane.WARNING_MESSAGE);
initFields();
}
}
I get errors even when typing in a date I know exists.
I think the problem lies with the for loop. It counts the number of elements, not total records.
This is stored in “numberOfOrdersInArray” int variable.
But if I do:
for (int i = 0; i < numberOfOrdersInArray.length; i++) {
I get the error ‘int cannot be dereferenced’
I hope I explained my problem well enough,
Many thanks for your help.
Use
.equals()for string comparison not==.==checks against references, while.equals()actually compares the string characters. This is important.EDIT: