Results 1 to 7 of 7
 11222012, 09:26 PM #1Member
 Join Date
 Nov 2012
 Posts
 14
 Rep Power
 0
Java Array Loop  Having Difficulties!!!
WHAT SHOULD BE HAPPENING: The purpose of this program is to get 10 numbers from the user, and then find the mode and median. Below is the first method that is activated. The goals of this method are to put each unique number into an array titled "input". Then, in a parallel array titled "frequency", it shows how many times that number in "input" was inputted by the user. Finally, "inputLength" determines the number of numbers in "input". All of these were declared before the main method.
WHAT ACTUALLY HAPPENS: The "input" array and "inputLength" variable work correctly, but every "frequency" array value is "1", even though it should be higher than that sometimes.
THE METHOD:
Java Code:for (int i = 0; i < 10; i++){ //Repeat this code 10 times double thisInput = Double.parseDouble(JOptionPane.showInputDialog("Please enter number #" + (i + 1))); //Get user input boolean alreadyExists = false; //Determines if thisInput already exists in numberList for (int x = 0; i < inputLength; x++){ //Loop through array if (input[x] == thisInput){ //If number is found in Array frequency[x]++; //Increase the frequency of that number alreadyExists = true; //Number exists break; //Stop looping through array } } if (alreadyExists == false){ //If the number did not already exist input[i] = thisInput; //Set thisInput into array frequency[i] = 1; //Set frequency of this number to 1 inputLength++; //Increase input length } alreadyExists = false; //Reset to false } }
THE ENTIRE PROGRAM:
Just in case you need to look at something quickly! :)
Java Code:import javax.swing.JOptionPane; public class class1004 { /** * @title Project 10.03 * Finds the mode and median of 10 integer inputs * @author ******************* * @date November 11, 2012 */ //Define Variables private static double input[] = new double[10]; //The list input by user private static double frequency[] = new double[10]; //The frequency of each number, parallel to "numberList" private static double medianArray[] = new double[10]; //The array used to find the median private static double mode; //The mode of the data found above private static double median; //The median of the data found above private static int inputLength = 0; //Shows the length of the Input array public static void main(String[] args) { gatherData(); //Gets data from user input determineMode(); //Uses data to determine mode putInOrderOfValue(); //Puts array in order of value determineMedian(); //Determines the median displayResults(); //Display the mode to user } public static void gatherData(){ for (int i = 0; i < 10; i++){ //Repeat this code 10 times double thisInput = Double.parseDouble(JOptionPane.showInputDialog("Please enter number #" + (i + 1))); //Get user input boolean alreadyExists = false; //Determines if thisInput already exists in numberList for (int x = 0; i < inputLength; x++){ //Loop through array if (input[x] == thisInput){ //If number is found in Array frequency[x]++; //Increase the frequency of that number alreadyExists = true; //Number exists break; //Stop looping through array } } if (alreadyExists == false){ //If the number did not already exist input[i] = thisInput; //Set thisInput into array frequency[i] = 1; //Set frequency of this number to 1 inputLength++; //Increase input length } alreadyExists = false; //Reset to false } } public static void determineMode(){ int greatest = 0; //Determines which array value is greatest for (int a = 0; a < inputLength; a++){ //Loop through input array if (frequency[a] > frequency[greatest]) //If this iteration has a higher value greatest = a;//Make it the new value } mode = input[greatest]; //Makes mode = to the value of the most frequent number } public static void putInOrderOfValue(){ //Find the smallest value medianArray[0] = input[0]; //Initialize the first input so the following loops will function correctly int placementOfSmallest; //Determines the location of the smallest variable placementOfSmallest = 0; for (int c = 1; c < inputLength; c++){ //following used to determine if (input[c] < medianArray[0]){ //If this value is smaller than the current smallest variable medianArray[0] = input[c]; //Set it as smallest placementOfSmallest = c; //Sets this location as the placement of the smallest value } } frequency[placementOfSmallest]; //Decrease the frequency of the smallest integer //Find the other values in order, starting with the smallest for (int b = 0; b < 10; b++){ //Repeat this code for each original input value, which is 10 times double thisValueFinal = input[0]; //The value for this iteration, to be determined below int thisValuePlacement = 0; //Determines the placement, in array "input", of this lowest value for (int c = 0; c < inputLength; c++){ //Loop through each array value to find the current smallest double thisValueTentative = input[c]; //Sets the tentative value to the current looped version of array "input" if (thisValueTentative < thisValueFinal && frequency[c] > 0){ //If the value exists and is lower than the current value thisValuePlacement = c; //Set the placement to c thisValueFinal = input[c]; //And get c's value in input array } } medianArray[b] = thisValueFinal; //Sets the array's b value to the final value variable frequency[thisValuePlacement]; //Decrease this value's frequency by 1 } //Now the array should be in order! } public static void determineMedian(){ double median1 = medianArray[4]; //Determines the first middle number double median2 = medianArray[5]; //Determines the second middle number median = (median1 + median2) / 2; //Find the average of the two middle numbers, THIS IS THE MEDIAN } public static void displayResults(){ JOptionPane.showMessageDialog(null, "Mode = " + mode + "\nMedian = " + median); //Displays results } }
 11232012, 02:18 AM #2Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 758
 Rep Power
 8
Re: Java Array Loop  Having Difficulties!!!
And what is your question? What is the difficulties that you have?
Website: Learn Java by Examples
 11232012, 04:50 AM #3Member
 Join Date
 Nov 2012
 Posts
 14
 Rep Power
 0
Re: Java Array Loop  Having Difficulties!!!
I outlined "What should be happening" and "What actually happens" above, before the code. I want to know how to solve "What actually happens" and make it fit "what should be happening". Essentially my code is bugged, I've tried debugging it, but as a new programmer I'm stumped.
 11232012, 05:12 AM #4Senior Member
 Join Date
 Nov 2012
 Posts
 105
 Rep Power
 0
Re: Java Array Loop  Having Difficulties!!!
I haven't read the whole code, but I'm going to assume it has something to do with: frequency[i] = 1;
 11232012, 06:15 AM #5Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 758
 Rep Power
 8
Re: Java Array Loop  Having Difficulties!!!
I think it is because your inner loop in the gatherData() method is never get executed. I think it should be "x < inputLength" and not "i < inputLength".
Java Code:for (int x = 0; i < inputLength; x++) { ... }
Website: Learn Java by Examples
 11232012, 02:33 PM #6Member
 Join Date
 Nov 2012
 Posts
 1
 Rep Power
 0
Re: Java Array Loop  Having Difficulties!!!
I think the code for the java in the gatherData() should read
for(int x=0;x<inputLength; x++)
{
//the suff goes here
}
 11252012, 04:09 PM #7Member
 Join Date
 Nov 2012
 Posts
 14
 Rep Power
 0
Similar Threads

Chrome/java version difficulties
By Daimoth in forum Java AppletsReplies: 8Last Post: 06042012, 01:21 AM 
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10012011, 02:12 AM 
Array Code difficulties
By NixasMuraki in forum New To JavaReplies: 2Last Post: 02082011, 12:17 AM 
Difficulties randomizing images in an array
By phb5004 in forum New To JavaReplies: 2Last Post: 12112009, 04:45 AM
Bookmarks