How to use!. String comparisons?

We are working on a group task. Part of the program is intended to be used to enter “payment”, followed by the SIN student number (social security number) and how much to deduct their account. Eg pay,193678187,900.

SIN SIN is compared with a list of files generated by the array and compares curSin.equals(stuSin)). curSinand stuSinare strings.

If a SIN is found, it will reduce the number of elements from the student’s balance (not shown) and write it to a file write(student,inrec).

If the SIN is not found, it should return "SIN not found."
Our problem is that even if a SIN is found, it still returns "SIN not found". We believe that this has something to do with the if statement, and the comparison is not equal, but we tried everything we can think about and cannot make it work properly.

Any help would be greatly appreciated. Thank.

Below is a snippet of code.

    if (cmd.equals("pay") && p.length == 3)
    { // first word is "pay";three terms (cmd, sin, $)
        System.out.println("pay");
        // Pay: decreases student payment, notifies of overpayment
        String stuSin = p[1];
        double stupay = Double.parseDouble(p[2]);
        for (int i = 0; i < student.length; i++)
        {
            Student x1 = student[i];
            String curSin = x1.getSin();
            if (curSin.equals(stuSin))
            {
                double curpay = x1.getBal();
                double newBal = curpay - stupay;
                if (stupay > curpay)
                {
                    JOptionPane.showMessageDialog(null, "Overpayment");
                    System.exit(0);
                }
                x1.setBal(newBal);
                System.out.println(x1.getBal());

                write(student, inrec);
            }
            else if (!curSin.equals(stuSin))
            {
                JOptionPane.showMessageDialog(null, "SIN not found"); // sin not found
                System.exit(0);
            }

        }// end of for loop
    }
+3
source share
2 answers

Nothing seems to have happened using the equals method.

You should add a couple of debugging statuses (or actually use a debugger) to make sure that you are really comparing the lines you want.

System.out.println("stuSin: '" + stuSin + "'");
System.out.println("curSin: '" + curSin + "'");
+3
source

, , .

. , , .

, , , .

, JOptionPane.showMessageDialog(null,"SIN not found"); //sin not found

+5

All Articles