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

# Thread: Program not working - help?

1. ## 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
{

double y2;
double y1;
double x2;
double x1;

System.out.println ("Enter y2:  ");

System.out.println ("Enter y1:  ");

System.out.println ("Enter x2:  ");

System.out.println ("Enter x1:  ");

Calculations calculate = new Calculations (x1, x2, y1, y2);

}
}

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;

{
return ("The solution to the equation (y2-y1) / (x2 - x1) is  " + solution + ".");
}

}```

Thanks

2. ## Re: Program not working - help?

The variables used in the calculations have not been assigned values when they are used in the expressions.

3. ## Re: Program not working - help?

So what could I do to make it work?

4. ## 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.

5. ## Re: Program not working - help?

I got it working. Thanks for the help!

6. ## Re: Program not working - help?

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