Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By wsaryada
  • 1 Post By LYEON

Thread: Java Array Loop - Having Difficulties!!!

  1. #1
    Captain Forge is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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
    	}
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Java Array Loop - Having Difficulties!!!

    And what is your question? What is the difficulties that you have?

  3. #3
    Captain Forge is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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.

  4. #4
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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;

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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++) {
    ...
    }
    Captain Forge likes this.

  6. #6
    LYEON is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default 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
    }
    Captain Forge likes this.

  7. #7
    Captain Forge is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: Java Array Loop - Having Difficulties!!!

    Thanks everyone. I found my (multiple) errors.

Similar Threads

  1. Chrome/java version difficulties
    By Daimoth in forum Java Applets
    Replies: 8
    Last Post: 06-04-2012, 01:21 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. Array Code difficulties
    By NixasMuraki in forum New To Java
    Replies: 2
    Last Post: 02-08-2011, 12:17 AM
  4. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •