0

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.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Tarkenfire
0

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.

0

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}
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.