1

我正在搜索具有开始日期的记录:[userinput],如果有任何匹配项,将显示该记录,问题是我的程序的性质对于所有匹配项都是必需的(可以有多个在同一个天)需要以某种方式显示。

这是我到目前为止所拥有的:

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

   }
}     

即使输入我知道存在的日期,我也会收到错误。

我认为问题在于 for 循环。它计算元素的数量,而不是总记录。

这存储在“numberOfOrdersInArray” int 变量中。

但如果我这样做:

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

我收到错误“无法取消引用 int”

我希望我能很好地解释我的问题,

非常感谢您的帮助。

4

3 回答 3

1

用于.equals()字符串比较。检查引用,同时实际比较字符串字符。这个很重要。 ====.equals()

编辑:

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

            }
于 2012-06-01T13:06:48.243 回答
1
 break; statement will be inside the if condition.

else {
          JOptionPane.showMessageDialog(null, "Order ID Doesn't Exist", "Error!", JOptionPane.WARNING_MESSAGE);
             //break;//Wrong HERE
}
于 2012-06-01T13:11:59.407 回答
1

我同意前面的回答。为什么你有一个break语句?一旦 if 子句中的某些内容不匹配,它就会从 for 循环中中断(break 语句对 if else 语句没有任何作用)。

但我也对你在这里所说的感到困惑:

“我认为问题在于 for 循环。它计算元素的数量,而不是总记录。

这存储在“numberOfOrdersInArray” int 变量中。”

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++) {
于 2012-06-01T13:21:08.887 回答