compiling error help?

compiling error help?
here is the program and it wont compile the starred out line is were the problem is and it gives the error message “cannot find symbol-Method toArray(double[])

import java.util.ArrayList;
import java.util.Scanner;

/**
* The StatsRule class allows the user to enter numbers (doubles) and then
* the program prints out the mean, max, min and range of those numbers. The
* user can enter as many numbers as they want to.
*
* For a four: the program also computes the mode.
* To have your name retired in the programming hall of fame: the program
* also computes the median value.
*
* This is a Mr. G original project
**/
public class StatsRule
{
public static void main(String [] args)
{
ArrayList bin = new ArrayList();
Scanner read = new Scanner(System.in);

double min, max, mean, mode, range, input, numone, numtwo, sum;
int count, size, countone;
boolean ismode = false;

System.out.println(“Please enter your desired data set then enter zero to calculate the statistics”);
input = read.nextDouble();

min = input;
max = input;
count = 0;

while(input != 0)
{
bin.add(input);
sum = sum + input;

if(input < min)
{
min = input;
}
if(input > max)
{
max = input;
}

System.out.println(“Please enter your desired data set then enter zero to calculate the statistics”);
input = read.nextDouble();

}
size = bin.size();
double[] cont = new double[size];
***bin.toArray(cont);

while(count < size)
{
numone = bin.get(count);
count ++;

while(countone < size)
{
count++;
numtwo = bin.get(countone);

if(numone == numtwo)
{
mode = numone;
ismode = true;
}
}
}

range = max - min;
mean = sum / bin.size();

System.out.println("The Max = " + max);
System.out.println("The Min = " + min);
System.out.println("The Range = " + range);
System.out.println("The Mean = " + mean);

if(ismode == true)
{
System.out.println("The mode = " + mode);
}
else
{
System.out.println("There is no mode");
}
}
}

Best answer:

Instead of:

bin.toArray(cont);

you want:

cont = bin.toArray();

But you will still have some type problems, toArray returns values of type Object[] but cont is of type double[]. Probably you should change the declared type of bin from ArrayList to ArrayList, then declare cont to be of type Double[], then cast the result of calling toArray to that type before assigning it to cont. At worst you could declare cont to be of type Object[], then you wouldn’t need the cast, but reading each value out of cont would have to be casted to Double.

A way around the casting problem, if you declared bin to be of type ArrayList, would be to just iterate over bin and copy each Double value out of bin and into cont in the loop, instead of using toArray.

Tags: , ,

Under Forum

1 Comment for compiling error help?

  • 1. C B  |  June 9th, 2009 at 9:23 am

    ArrayList uses a generic, so declare bin like this:

    ArrayList bin = new Arraylist();

Leave a Comment for compiling error help?

Required

Required, hidden

Trackback this post  |  Subscribe to the comments via RSS Feed


Forum