need help java program wont compile?
need help java program wont compile?
the below java program is for an ap comp sci class and when i try to compile it says cannot find symbol – method get(double)
import java.util.Scanner;
import java.util.ArrayList;
/**
* 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, not found in the text.
*
* @author (007)
* @version (1.0)
*/
public class StatsRule
{
public static void main(String [] args)
{
Scanner read = new Scanner(System.in);
ArrayList Bin = new ArrayList();
ArrayList OrderedBin = new ArrayList();
double count, selection, tempmode, min, max, mode, median, mean, minlistorder, maxlistorder, medianlow, medianhigh, input, range, total;
System.out.println(“Please enter your values then press 0 to calculate. “);
input = read.nextDouble();
min = input;
max = input;
count = 0;
minlistorder = 0;
maxlistorder = 0;
while(input != 0)
{
System.out.println(“Please enter your values then press 0 to calculate. “);
input = read.nextDouble();
Bin.add(input);
total = total + input;
count++;
tempmode = input;
*****if(Bin.get(count) < min)
{
min = Bin.get(count);
minlistorder++;
orderedbin.add(minlistorder, min);
}
if(Bin.get(count) > max)
{
max = Bin.get(count);
maxlistorder++;
orderedbin.add(maxlistorder, max);
}
if(Bin.get(count) == tempmode)
{
mode = tempmode;
}
}
medianlow = (orderedbin.get(count – 1));
medianhigh = (orderedbin.get(count + 1));
median = (medianhigh + medianlow) / 2;
mean = total / bin.size();
range = max – min;
System.out.printline(“range = ” + range + “\n min = ” + min + “\n max = ” + max + “\n mode = ” + mode + “\n mean = ” + mean + “\n median = ” + median);
}
}
Best answer:
Its because your passing Count into Bin.Get( ) Method
Count as been specified as a Double primative type.
Bin.Get( ) takes an int, since Bin is of type arrayList and arraylist.get() takes an int, Documentation msdn.microsoft.com/en-us/library/aa990348(VS.80).aspx
you will need to type case Count into an integer, Or reconsider why you even ever made Count a double , and declare it from the start as a integer.
Tags: compile, HELP, java, need, program, wont
Under Forum

2 Comments for need help java program wont compile?
1. Patrick D | September 27th, 2007 at 10:56 pm
Count should be declared as a int not a double
2. wanswong | September 27th, 2007 at 11:17 pm
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StatsRule { Bin = new ArrayList ();
List
public void addBin(double value) {
Bin.add(value);
}
public double getMin() {
return Collections.min(Bin);
}
public double getMax() {
return Collections.max(Bin);
}
public double getMean() {
double retval = 0.0;
// if the array is blank return 0
if(Bin.isEmpty())
return retval;
for(double val:Bin)
retval += val;
retval /= (double)Bin.size();
return retval;
}
public double getRange() {
double retval = 0.0;
// if the array is blank return 0
if(Bin.isEmpty())
return retval;
Collections.sort(Bin);
retval = Bin.get(Bin.size()-1) – Bin.get(0);
return retval;
}
public double getMode() {
// if the array is blank return 0
if(Bin.isEmpty())
return 0.0;
Collections.sort(Bin); it = Bin.iterator();
double prevValue = 0, value = 0, curValue = 0;
int prevCount = 0, count = 0;
boolean start = true;
Iterator
while (it.hasNext()) {
curValue = it.next();
if(start) {
value = curValue;
count++;
start = false;
}
else {
if(curValue == value)
count++;
else {
if(count > prevCount) {
prevValue = value;
prevCount = count;
}
value = curValue;
count = 1;
}
}
}
if(count > prevCount)
return value;
else if(prevCount == 1)
return Double.MIN_VALUE; // no mode
return prevValue;
}
public double getMedian() {
// if the array is blank return 0
if(Bin.isEmpty())
return 0.0;
Collections.sort(Bin);
int mid = Bin.size()/2;
if(Bin.size() % 2 == 0)
return ((Bin.get(mid-1) + Bin.get(mid))/2.0);
return Bin.get(mid);
}
/**
* @param args
*/
public static void main(String[] args) {
StatsRule sr = new StatsRule();
Scanner read = new Scanner(System.in);
System.out.println(“Please enter your values separated by spaces: “);
String vals = read.nextLine();
String[] tokens = vals.split(” “);
for(String token: tokens) {
sr.addBin(Double.parseDouble(token));
}
System.out.println(“Min: ” + sr.getMin());
System.out.println(“Max: ” + sr.getMax());
System.out.println(“Mean: ” + sr.getMean());
System.out.println(“Range: ” + sr.getRange());
System.out.println(“Mode: ” + (sr.getMode() == Double.MIN_VALUE ? “none” : sr.getMode()));
System.out.println(“Median: ” + sr.getMedian());
}
}
Leave a Comment for need help java program wont compile?
Trackback this post | Subscribe to the comments via RSS Feed