Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

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

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    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"))
        	{
        	}
        	}
     
     
    }
    }
    Last edited by Norm; September 29th, 2012 at 07:26 PM. Reason: added code tags


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: when i run the program to find the y values it always prints y2 instead

    Quote Originally Posted by jamesR View Post
    ...it always prints y2 instead...
    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
                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

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: when i run the program to find the y values it always prints y2 instead

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: when i run the program to find the y values it always prints y2 instead

    Quote Originally Posted by jamesR View Post
    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
    Last edited by Zaphod_b; September 29th, 2012 at 10:52 PM.

  6. The Following 2 Users Say Thank You to Zaphod_b For This Useful Post:

    curmudgeon (September 29th, 2012), jamesR (September 29th, 2012)

  7. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: when i run the program to find the y values it always prints y2 instead

    Quote Originally Posted by Zaphod_b View Post
    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.

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: when i run the program to find the y values it always prints y2 instead

    Quote Originally Posted by jamesR View Post
    ...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:

            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
    Last edited by Zaphod_b; September 30th, 2012 at 08:42 AM.

Similar Threads

  1. Program returning wrong values.
    By cam25 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2012, 11:59 PM
  2. recursive program to find median
    By TeamRival in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2011, 10:40 PM
  3. [SOLVED] Could someone help me find my errors on this program?? (homework)
    By r19ecua in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2011, 10:25 PM
  4. Replies: 3
    Last Post: February 23rd, 2011, 01:27 AM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM

Tags for this Thread