Results 1 to 4 of 4
  1. #1
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Question Print Array as String through class file?

    Trying to figure out how to display the reordered array and the average from toString() method through the second file. The second file has the main. Another homework help.
    Java Code:
    import java.util.Scanner;
    
    average //This is the Average class
    {
    	private int[] data = new int[5];  //contains the scores
    	private double mean = 0;  //arithmetic average of the scores
    	private int total;
    
    	/**
    	Constructor - allocate memory for the array.  Create a loop to gather all of the scores to be stored
    	will call the methods to sort and handle the collected data
    	*/
      	average()
     	{
     		Scanner keyboard = new Scanner(System.in);
     		for (int number = 0; number < data.length; number++)
     		{
     			System.out.println("Please input number "+(number+1)+".");
     			data[number] = keyboard.nextInt();
     		}
     		calculateMean(data);
     		selectionSort(data);
     		toString();
      	}
    
      	/**
      	calculateMean method will calculate the mean by adding it to a running total and then dividing by the quantity of the elements
      	*/
      	public void calculateMean(int[] array)
      	{
    		for (int number = 0; number < array.length; number++)
    		{
    			total =+ array[number];
    		}
    		mean = total / array.length;
      	}
    
      	/**
      	Returns the calculateMean data as String
      	and the selectionSort as String
      	*/
      	public void toString(int[] array)
      	{
    		for (int number = 0; number < array.length; number++)
    		{
    			System.out.println(array[number]);
    		}
    		System.out.println("Mean: "+mean);
      	}
    
      	/**
      	selectionSort reorganizes the data from highest to lowest
      	*/
      	public static void selectionSort(int[] array)
      	{
    		int startScan, index, minIndex, minValue;
    		for (startScan = 0; startScan<(array.length-1); startScan++)
    		{
    			minIndex = startScan;
    			minValue = array[startScan];
    			for (index = startScan + 1; index < array.length; index++)
    			{
    				if (array[index] < minValue)
    				{
    					minValue = array[index];
    					minIndex = index;
    				}
    			}
    			array[minIndex] = array[startScan];
    			array[startScan] = minValue;
    		}
      	}
    }
    Second file:
    Java Code:
    public class driverAverage
    {
       public static void main (String[ ] args)
    	{
    		System.out.println("This program will help you figure out the average age of a group of drivers.");
    		average averageDriver = average();
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    how to display the reordered array and the average from toString() method through the second file
    Do you mean class when you say file? Files can contain more than one class.
    To call the toString() method of the average class from the main() method in the driverAverage class, code: averageDriver.toString();
    If the toString() method returns a String, you can display it with the println() method like what's in the main() method
    System.out.println(averageDriver.toString());

  3. #3
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Default

    That's what I meant, but I already have the methods called in the average class. Why doesn't it display when the constructor calls the methods. So the second class starts a new instance of average and in the constructor it calls the methods to display the average and the reordered list. Why doesn't it show up?

  4. #4
    ocarabal is offline Member
    Join Date
    Jun 2010
    Location
    HI
    Posts
    9
    Rep Power
    0

    Default

    Well don't worry about answering this question I figured it out. I wasn't returning the data so that the other methods could retrieve them, but thanks though.

Similar Threads

  1. String array from file to ComboBox
    By cselic in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2010, 05:29 PM
  2. Print String from an Array
    By adityasirohi in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 04:10 PM
  3. I can only print string literals
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 01:20 AM
  4. Replies: 3
    Last Post: 03-19-2009, 05:00 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •