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 6 of 6

Thread: Program not working - help?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program not working - help?

    So this is one of the practice questions for my computer science exam tomorrow. For some reason, whatever numbers I enter, the result comes up with this:

    Enter y2:
    5
    Enter y1:
    2
    Enter x2:
    4
    Enter x1:
    2
    The solution to the equation (y2-y1) / (x2 - x1) is NaN.
    Why is the answer always NaN? Someone tell me what I'm doing wrong? Here is the code:



    import java.io.*;
     
    class questionone
    {
     
     
     
        public static void main (String [] args) throws IOException
        {
            InputStreamReader inStream = new InputStreamReader (System.in);
            BufferedReader stdin = new BufferedReader (inStream);
     
            double y2;
            double y1;
            double x2;
            double x1;
     
            System.out.println ("Enter y2:  ");
            y2 = Double.parseDouble (stdin.readLine ());
     
            System.out.println ("Enter y1:  ");
            y1 = Double.parseDouble (stdin.readLine ());
     
            System.out.println ("Enter x2:  ");
            x2 = Double.parseDouble (stdin.readLine ());
     
            System.out.println ("Enter x1:  ");
            x1 = Double.parseDouble (stdin.readLine ());
     
            Calculations calculate = new Calculations (x1, x2, y1, y2);
     
     
     
            System.out.println (calculate.receive());
     
        }
    }
     
     
    class Calculations
    {
     
     
        double x1;
        double x2;
        double y1;
        double y2;
     
        Calculations (double x1, double x2, double y1, double y2)
        {
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
        }
     
     
        double numerator = y2 - y1;
        double denominator = x2 - x1;
        double solution = numerator / denominator;
     
        String receive ()
        {
            return ("The solution to the equation (y2-y1) / (x2 - x1) is  " + solution + ".");
        }
     
    }

    Thanks


  2. #2
    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: Program not working - help?

    The variables used in the calculations have not been assigned values when they are used in the expressions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not working - help?

    So what could I do to make it work?

  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: Program not working - help?

    Use the values passed to the constructor in the expressions that are used to compute and assign values to the variable that is displayed in the receive() method.

    The current code uses the variables BEFORE they are given values in the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not working - help?

    I got it working. Thanks for the help!

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Location
    SPAIN
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not working - help?

    Post your wwwwwwworkin' code for those who might have your same problem

Similar Threads

  1. Program not working
    By KSR in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 19th, 2013, 04:31 AM
  2. Simple Calcular Program, But not working!!!
    By achugr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2012, 02:53 PM
  3. Cannot get buttons working in GUI program
    By Yeshibo in forum AWT / Java Swing
    Replies: 3
    Last Post: April 23rd, 2012, 12:59 PM
  4. Client Server Program Not Working
    By ROHIT C. in forum Java Networking
    Replies: 3
    Last Post: September 3rd, 2010, 02:30 PM
  5. Substring program, not working
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2009, 12:46 PM