I'm doing a rather convoluted project for a class regarding looping structures and have run into a novice-ish logic error that I can't figure out.

The error being that the following loop never ends:

//get transaction values and perform actions.
            try
            {
                sentinalValue = Double.parseDouble(JOptionPane.showInputDialog
                                    ("Enter transaction now. Enter \"-1\" to " +
                                    "end transaction group."));
            }
            catch(java.lang.NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "Only numeric values may be" +
                                            " entered. A value of 0 is assumed.");
            }
            catch(java.lang.NullPointerException e)
            {
                //soft exit on cancel.
                System.exit(0);
            }

            while(sentinalValue != -1.0)
            {
                if(isDeposit == true)
                {
                    totalTransaction = totalTransaction + sentinalValue;
                    balance = balance + sentinalValue;
                }
                else
                {
                    totalTransaction = totalTransaction - sentinalValue;
                    balance = balance - sentinalValue;
                }

                try
                {
                    sentinalValue = Double.parseDouble(JOptionPane.showInputDialog
                                    ("Enter transaction now. Enter \"-1\" to " +
                                    "end transaction group."));
                }
                catch(java.lang.NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "Only numeric values may be" +
                                            " entered. A value of 0 is assumed.");
                }
                catch(java.lang.NullPointerException e)
                {
                    //soft exit on cancel.
                    System.exit(0);
                }
                //debugging line
                System.out.println(sentinalValue);
                
            }

I have the feeling it's a stupid mistake I made due in part to my inexperience with this language, but any help (or even simple advice) would be appreciated.

Recommended Answers

All 4 Replies

Whenever I see an == or a != comparison involving a float, I figure that's likely to be the problem.

If negative values are prohibited, try sentinelValue<0 as your loop condition and see if that does it.

Yeah, sentinalValue >= 0 fixed it.

Many thanks.

Are you hip to why you don't want exact comparisons of floats and doubles? It's worth reading up on floating point numbers if you're not clear on why this was a problem.
One standard trick if you need to check equality of floats is to set a constant,

final double TOLERANCE = 0.000001; //or whatever

and then use

if (Math.abs(oneDouble-anotherDouble)<TOLERANCE) { //they're as equal as they need to be}

A lecture point on one of my class lectures hit me the second you said float, so, yeah, I understand why what I did didn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.