简体   繁体   中英

Displaying Multiple Results Array Search

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:

//instead of a count, you could also use a boolean
for (int i = 0, count= 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]);
                    count++;
                } 
                if(i == orderID.length - 1 && count==0){
                    JOptionPane.showMessageDialog(null, "Order ID Doesn't Exist", "Error!", JOptionPane.WARNING_MESSAGE);
                    break;
                }

            }
 break; statement will be inside the if condition.

else {
          JOptionPane.showMessageDialog(null, "Order ID Doesn't Exist", "Error!", JOptionPane.WARNING_MESSAGE);
             //break;//Wrong HERE
}

I agree with the previous answer. Why do you have a break statement? As soon as something doesn't match in your if clause it breaks from the for loop (break statements do nothing for if else statements).

But also I'm confused about what you said here:

"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."

Is numberOfOrdersInArray an int variable? In that case why are you using numberOfOrdersInArray.length? The .length is a variable that is held by arrays which is why it works if you have arrayName.length. But if numberOfOrdersInArray is just a counter int variable, then use it by itself so it looks like:

for (int i = 0; i < numberOfOrdersInArray; i++) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM