[ATTACH]numbers.txt[/ATTACH]
I think I just uploaded it. I know for a fact that the two modes for this text file are 10 and 48. When you ran your version of numbers, it gave you the correct mode? That is odd.
In researching the solution for this problem, I actually found that you cannot remove inside of a for loop, so I revised me code as well. If you can correct the problem on either one and make the modes (10, 48) actually appear, I will be eternally grateful, since I've been working on this for 5 days straight with no success. However, I know some methods such as hash and etc are more effective, my assignment requires that I stick with only array lists and no more importations... Thank you.
import java.util.ArrayList; import java.lang.Math; public class Statistics { private ArrayList<Integer> numbers = new ArrayList<Integer>(); private ArrayList<Integer> occurrences =new ArrayList<Integer>(); private ArrayList<Integer> finalMode = new ArrayList<Integer>(); private ArrayList<Integer> backUp = new ArrayList<Integer>(); public Statistics() { } //end default constructor public Statistics(ArrayList<Integer> inputNumbers) { numbers = inputNumbers; } //end non-default constructor public double calcAverage() { int total = 0; for(int currentNumber : numbers) //don't need to specify size cuz goes for duration of arraylist { total += currentNumber; } //end extended for loop return (total/numbers.size()); } //end method public double calcStandardDeviation() { double total = 0; for(int currentNumber : numbers) { double partOfSum = Math.pow((currentNumber - calcAverage()), 2); total += partOfSum; } //end extended for return Math.pow((total/(numbers.size() - 1)), 0.5); } //end method public void calcOccurrences() { for(int currentNumber : numbers) { int counter = 0; int numberOfOccurrences = 0; while (counter < numbers.size()) { if (currentNumber == numbers.get(counter)) { numberOfOccurrences++; counter++; } else { counter++; } //end if } //end while statement occurrences.add(numberOfOccurrences); backUp.add(numberOfOccurrences); } //end extended for loop } //end method public void calcFinalMode() { for(int currentOccurrence : occurrences) { int counter = 0; while (counter < occurrences.size() && counter > 0) { if (currentOccurrence >= occurrences.get(counter)) { counter++; } else { numbers.set((occurrences.indexOf(currentOccurrence)), 0); backUp.set((occurrences.indexOf(currentOccurrence)), 0); counter = 1000; } //end LONG if } // end while loop if (!(currentOccurrence == 0)) { int facto = numbers.get(backUp.indexOf(currentOccurrence)); finalMode.add(facto); } // end if int facto=0; } //end extended for loop } //end method public int calcDefMode() { int counter = 0; int facto = 0; while (counter < finalMode.size()) { if (finalMode.get(counter) > 0) { facto = finalMode.get(counter); counter = 1000; } //end if statment } //end while return facto; } //end method } //end class DRIVER -------------------------------------------------------- import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class StatisticsDriver { public static void main(String args[]) { ArrayList<Integer> numbers = new ArrayList<Integer>(); Scanner in; try { in = new Scanner(new File("numbers.txt")); while(in.hasNextInt()) { numbers.add(in.nextInt()); } //end while loop } catch(IOException i) { System.out.println("Error: " + i.getMessage()); } //end file-read Statistics myStatistics = new Statistics(numbers); //why can't put "ArrayList inputNumbers"???? System.out.println("Average: " + myStatistics.calcAverage()); System.out.println("Standard Deviation: " + myStatistics.calcStandardDeviation()); myStatistics.calcOccurrences(); myStatistics.calcFinalMode(); //System.out.println("Mode: " + myStatistics.calcFinalMode() + ", " + myStatistics.calcOtherMode()); System.out.println("Mode: " + myStatistics.calcDefMode()); }// end main } //end class