Mode Calculator
calculate mode of a set of numbers
import java.util.Scanner;
public class ModeCalculator {
public static void main(String[] args) {
String userInputStr;
double mode, maxValue;
int numofNum;
Scanner inputStream = new Scanner(System.in);
System.out.print("how many values would you like to find the mode of?");
userInputStr = inputStream.nextLine();
numofNum = Integer.parseInt(userInputStr);
double[] nums = new double [numofNum]; //creates array with doubles, numofNum number of values in the array
for (int i=0; i < numofNum; i++)
{
System.out.print("\nenter value: ");
userInputStr = inputStream.nextLine();
nums[i] = Double.parseDouble(userInputStr);
System.out.print(nums[i]);
}
mode = modecalc(nums);
System.out.print("\nthe mode is " +mode);
}
static double modecalc(double[] nums) {
double maxValue = 0.0;
int maxCount = 0, i, j;
for (i=0; i < nums.length; i++) {
int count = 0;
for (j=0; j < nums.length; j++) {
for (j = 0; j < nums.length; ++j) {
if (nums[j] == nums[i])
++count;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = nums[i];
}
}
return maxValue;
}
}
ModeCalculator.main(null);