# when i run the program to find the y values it always prints y2 instead

• September 29th, 2012, 12:15 PM
jamesR
when i run the program to find the y values it always prints y2 instead
Code :

```import javax.swing.*; public class LinearFinder { public LinearFinder() { } public static void main(String args[]) { int x1; int y1; int x2; int y2; String input = JOptionPane.showInputDialog("enter the first x coordinate"); x1 = Integer.parseInt(input); String input2 = JOptionPane.showInputDialog("enter the first y coordinate"); y1 = Integer.parseInt(input2); String input3 = JOptionPane.showInputDialog("enter the second x coordinate"); x2 = Integer.parseInt(input3); String input4 = JOptionPane.showInputDialog("enter the second y coordinate"); y2 = Integer.parseInt(input4);   // m holds the value for the slope double m; m = (y2 - y1) / (x2 - x1); // b holds the value for the y intercept double b; if(x1 > x2 && y1 != y2) { b = y2 - (x2 * m); System.out.println("The equation of the linear function is: y="+ m +("x+")+b); String input5 = JOptionPane.showInputDialog("Would you like to continue and use the equation to find y values?(yes or no)"); if(input5.equals("yes")) { double y; double d;       String input8 = JOptionPane.showInputDialog("Enter the 'x' value"); d = Double.parseDouble(input3);   y = m * d + b;   System.out.println("The y value for your equation is " + y); } else if(input5.equals("no")) { } } else if(x2 > x1 && y1 != y2) { b = y1 - (x1 * m); System.out.println("The equation of the linear function is: y="+ m +("x+")+b); String input5 = JOptionPane.showInputDialog("Would you like to continue and use the equation to find y values?(yes or no)"); if(input5.equals("yes")) { double y;   double d;     String input8 = JOptionPane.showInputDialog("Enter the 'x' value"); d = Double.parseDouble(input3);   y = m * d + b;   System.out.println("The y value for your equation is " + y); } else if(input5.equals("no")) { } } else if(x1 == x2 && y2 != y1) { System.out.println("invalid coordinates system must be a function"); } else if(x1 != x2 && y1 == y2) { System.out.println("The function is constant at y="+y1); String input5 = JOptionPane.showInputDialog("Would you like to continue and use the equation to find y values?(yes or no)"); if(input5.equals("yes")) { double y;   double d;     String input8 = JOptionPane.showInputDialog("Enter the 'x' value"); d = Double.parseDouble(input3); b = y1; y = m * d + b;   System.out.println("The y value for your equation is " + y); } else if(input5.equals("no")) { } }     } }```
• September 29th, 2012, 03:13 PM
Zaphod_b
Re: when i run the program to find the y values it always prints y2 instead
Quote:

Originally Posted by jamesR

Well, look (really: look) at the place(s) where it gets values that seem not to be used correctly. If you still don't see a problem, make the program show you what it is using at each place things go bad.

Maybe something like the following
Code java:

``` String input8 = JOptionPane.showInputDialog("Enter the 'x' value"); d = Double.parseDouble(input3); System.out.println("d = " + d); System.out.println("m = " + m); System.out.println("b = " + b);   y = m * d + b;   System.out.println("The y value for your equation is " + y);```

Cheers!

Z
• September 29th, 2012, 07:20 PM
jamesR
Re: when i run the program to find the y values it always prints y2 instead
ok so when i did what you suggested, d was always 3.
• September 29th, 2012, 07:25 PM
Norm
Re: when i run the program to find the y values it always prints y2 instead
Quote:

d was always 3.
Is that what d was supposed to be or did you expect something different?
Please explain what d was supposed to be.
• September 29th, 2012, 10:28 PM
Zaphod_b
Re: when i run the program to find the y values it always prints y2 instead
Quote:

Originally Posted by jamesR
ok so when i did what you suggested, d was always 3.

The first thing that I suggested was to look, really look, at the code.

What is the line where you get a user input String from the input dialog box? Go find a pencil and paper and write it down.

Show us that line here:

What is the line where you obtain the numerical value of d from a String? Use pencil and paper to write write that line.

Show us that line here:

Now look (really look) at those two lines all by themselves on your paper.

Do you still not see the problem?

Cheers!

Z
• September 29th, 2012, 10:52 PM
jamesR
Re: when i run the program to find the y values it always prints y2 instead
Quote:

Originally Posted by Zaphod_b
The first thing that I suggested was to look, really look, at the code.

What is the line where you get user input String from the input dialog box? Go find a pencil and paper and write it down.

Show us that line here: String input8 = JOptionPane.showInputDialog("Enter the 'x' value");

What is the line where you obtain the numerical value of d from a String? Use pencil and paper to write write that line.

Show us that line here:d = Double.parseDouble(input3);

Thank you so much for all your help Zaphod, i have no idea how i din't see that simple error earlier.

Now look (really look) at those two lines all by themselves on your paper.

Do you still not see the problem?

Cheers!

Z

Thank you so much for all your help Zaphod, i have no idea how i din't see that simple error earlier.
• September 29th, 2012, 11:00 PM
jamesR
Re: when i run the program to find the y values it always prints y2 instead
sorry, I wasn't ignoring your question, I was away from my computer for a while.
• September 30th, 2012, 08:38 AM
Zaphod_b
Re: when i run the program to find the y values it always prints y2 instead
Quote:

Originally Posted by jamesR
...din't see that simple error...

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.

For example:

Code java:

``` if(input5.equals("yes")) { 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.

Cheers!

Z