Needing some help with a small problem. I have this program that is not compiling correctly. The instructions are simple which are: The PairTest should prompt the user for the two values, create a Pair object with the values and then print the average, distance, maximum, and minimum of the pair.

In theory, the output should look something like this:

Enter the first number:
5.5
Enter the second number:
3.0
Average: 4.25
Distance: 2.5
Maximum: 5.5
Minimum: 3.0

Here is what I have so far.

import java.lang.Math;
import java.util.Scanner;

public class PairTest
{
    private static double num1;
    private static double num2;
    private static double average = (num1+num2)/2.0;
    private static double distance = Math.abs(num1-num2);
    private static double maximum = Math.max(num1, num2);
    private static double minimum = Math.min(num1, num2);
    private static Scanner in;


    public static void main(String args[])
    {

    in = new Scanner(System.in); 

        System.out.println("Please enter a value for the first number: ");
        num1 = in.nextDouble();

        System.out.println("Please enter a value for the second number: ");
        num2 = in.nextDouble();


    System.out.println("Average: " + average);
    System.out.println("Distance: " + distance);
    System.out.println("Maximum Number: " + maximum);
    System.out.println("Minimum Number: " + minimum);

    }
  }   
}

Would someone be so kind to explain to me what I am doing wrong? I would greatly appreciate it.

The error that it is giving me is:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at PairTest.main(PairTest.java:15)

Thank you.

Problem is because you do this: num1 = null; num2 = null; and you are calculating average with two null's you can't do that.

You need to do like this:

    import java.lang.Math;
    import java.util.Scanner;
    public class PairTest
    {
    private static double num1;
    private static double num2;
    private static double average;
    private static double distance;
    private static double maximum;
    private static double minimum;
    private static Scanner in;
    public static void main(String args[])
    {
    in = new Scanner(System.in);
    System.out.println("Please enter a value for the first number: ");
    num1 = in.nextDouble();
    System.out.println("Please enter a value for the second number: ");
    num2 = in.nextDouble();

    average = (num1+num2)/2.0;
    distance = Math.abs(num1-num2);
    maximum = Math.max(num1, num2);
    minimum = Math.min(num1, num2);

    System.out.println("Average: " + average);
    System.out.println("Distance: " + distance);
    System.out.println("Maximum Number: " + maximum);
    System.out.println("Minimum Number: " + minimum);
    }
    }
    }

So you declare average as double and you can't know num 1 and num2 so you need to read values from scaner and then calculate it when you get num1 and num2...

That totally makes sense. But just a question I thought you could do calculations in the area where you initialize the variables?

You can do calculations when initialising variables, but only using values that are known at the time, eg
int a = 3;
int b = a+1;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.