Originally Posted by jamesR
Been there; done that. About a million times. Maybe more.
Here's a thought:
Maybe it's time to think about using meaningful names for variables.
String xStr = JOptionPane.showInputDialog("Enter the 'x' value");
double x = Double.parseDouble(xStr);
double y = m * x + b; // m is the slope, b is the y-intercept
System.out.println("The y value for your equation is " + y);
See how it goes? The equation from the math book is probably y = m x + b
, and I remind myself (with a comment) what the variables mean in the equation. More importantly, I create names for the user response String and the resulting numeric variable that let me see at a glance what is going on. Additionally, by declaring them locally, I am sure that they won't be used improperly outside that block if I absent-mindedly (or through a typographical error) end up with that same variable name outside the block.
These issues are, of course, style rather than substance, and the program you posted would have worked just fine if you didn't have the not-so-obvious-until-someone-points-it-out typographical error.
I'm not sure that the importance of naming is stressed enough in beginning courses and tutorials. This is not a Small Thing; it is a Big Thing when you get beyond simple, short throwaway programs like school assignments or exercises from books or tutorials.
It is really important, not only for debugging as you are developing the program, but clarity resulting from meaningful names is invaluable for maintenance of programs that are going to be around for a while. (And using meaningful names costs nothing. I probably would have used a name other than input5
for the user's yes/no response at the beginning of your code snippet, but I'll leave that up to you.)
In this program, using x1,y1 and x2,y2 for names of the points and using m for the slope and b for the y-intercept is OK with me, since those are the variable names typically given in math books where equations for lines are discussed. On the other hand, where did the idea for "d" come from? Why not use x? And 'input3', 'input8' ... well I guess you get the idea.