I am having trouble getting output from this program. The objective of the program is to allow the user to enter 5 scores and then it is supposed to calculate the mean and rearrange the data set from highest to lowest.

I am supposed to run it through the AverageDriver class. The main method is only supposed to declare and instantiate an Average object. The Average object information should then be printed to the console. It should output the data set from highest to lowest and the mean.

Please help me find any mistakes I've been trying this for hours and I cant seem to figure it out. Any input would be greatly appreciated.

import java.util.Scanner;

public class Average 
{
	private int data[];
	private double mean;
	
	public Average()
	{
		//Create a Scanner object for keyboard input
		Scanner keyboard = new Scanner(System.in);
		
		final int SCORES = 5; //Total number of scores
		
		//Create an array to hold scores entered by the user.
		data = new int[SCORES];
		
		System.out.println("Please enter the " + SCORES + " scores.");
		
		//Cycle through the array, getting each score.
		for (int i = 0; i < SCORES; i++)
		{
			System.out.println("Score " + (i + 1) + ": ");
			data[i] = keyboard.nextInt();	
		}
		
		calculateMean();
		selectionSort();		
	}
		
	public void calculateMean()
	{
		double total = 0;
		
		for (int i = 0; i < data.length; i++)
        	total += data[i];

        mean = (total / data.length);
	}
	
	public void selectionSort()
	{
		int highest = data[0];
		for (int i = 1; i < data.length; i++)
		{
			if(data[i] > highest)
				highest = data[i];
		}
	}
	
		public String toString()
	{
			String str = ("Data: " + data + "\nMean: " + mean);
			return str;
	}
	
}
public class AverageDriver 
{
	public static void main(String[] args) 
	{
		Average object = new Average();
		object.toString();		
	}
}

Well you could have at least mentioned what problem you are encountering and what output you are currently getting so we can check where you are actually going wrong .... instead of going through the entire code.

Now first mistake which I found :-

String str = ("Data: " + data + "\nMean: " + mean);

"data" is an array, you cannot directly print the contents of an array like that. You need to iterate through each index of the array and then append it to the string which you are printing out. Kind of like :-

int myArray[] = new int[...];
.
.
.
String dataToPrint=new String("");
for (int i=0; i<myArray.length; i++)  {
  dataToPrint = dataToPrint + myArray[i];
}

Better use

String str = ("Data: " + Arrays.toString(data) + "\nMean: " + mean);

Also your returning "str" in the toString method but you are not printing it in the main function of AverageDriver.

In your launcher you have to put the toString into a System.out.print or something like that for example:

public class AverageDriver 
{
	public static void main(String[] args) 
	{
		Average object = new Average();
		System.out.println(object.toString());		
	}
}

or

import java.swing.*;
public class AverageDriver 
{
	public static void main(String[] args) 
	{
		Average object = new Average();
		JPaneOption.showMessageDialog(null, object.toString());		
	}
}

In your launcher you have to put the toString into a System.out.print or something like that for example:

public class AverageDriver 
{
	public static void main(String[] args) 
	{
		Average object = new Average();
		System.out.println(object.toString());		
	}
}

or

import java.swing.*;
public class AverageDriver 
{
	public static void main(String[] args) 
	{
		Average object = new Average();
		JPaneOption.showMessageDialog(null, object.toString());		
	}
}

You are correct in what you say but I would like to add that you can omit calling the toString method:

public class AverageDriver 
{
	public static void main(String[] args) 
	{
		Average object = new Average();
		System.out.println(object);		
	}
}

The toString method is called automatically.

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.