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

    Default Java Array Error

    I don't get why I'm getting an error in this class, can someone help me? It keeps saying:
    checkpoint 1
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at mainClass1002.displayResults(mainClass1002.java:49 )
    at mainClass1002.main(mainClass1002.java:19)
    checkpoint 2
    checkpoint 3
    Here is the code, for debugging purposes I placed the 4 "checkpoints". Please note that I'm new to programming, so this might not be too readable.

    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class mainClass1002 {
    
    	/**
    	 * @title Project 10.02
    	 * Takes 10 numbers as input, finds the average, and lists the ones that are larger than the average.
    	 * @date November 10, 2012
    	 * @author <Censored> 
    	 */
    	public static void main(String[] args) { //Main method
    		int[] input = gatherUserData(); //Gets data from user
    					System.out.println("checkpoint 1");
    		double average = getAverage(input); //Gets average of data
    					System.out.println("checkpoint 2");
    		int[] greaterThanAverage = getGreater(input, average); //Gets those greater than average
    					System.out.println("checkpoint 3");
    		displayResults(average, greaterThanAverage); //Displays results
    					System.out.println("checkpoint 4");
    	}
    	
    	public static int[] gatherUserData(){
    		int[] input = new int[10]; //The array of input from user
    		for (int i = 0; i < 10; i++)
    			input[i] = (int)Double.parseDouble(JOptionPane.showInputDialog("Please enter number #" + i)); //Gets 10 inputs
    		return input;
    	}
    	
    	public static double getAverage(int[] input){
    		double average = 0; //The average to be used
    		for (int i = 0; i < 10; i++)//Repeat this code 10 times
    			average += input[i]; //Add each array # to variable
    		average /= 10; //Divide the number by 10 to get average
    		return average;
    	}
    	
    	public static int[] getGreater(int[] input, double average){
    		int[] greaterThanAverage = new int[10]; //List of numbers greater than average
    		for (int i = 0; i < 10; i++) //Perform action 10 times
    			if (input[i] > average) //If that number is greater than average
    				greaterThanAverage[i] = input[i]; //Add it to array "greaterThanAverage"
    		return greaterThanAverage;
    	}
    	
    	public static void displayResults (double average, int[] greaterThanAverage){
    		String results = "Average = " + average + "\n" + "Numbers greater than average: \n"; //The string to display
    		int i = 0; //Iterations
    		while (greaterThanAverage[i] >= 0 || greaterThanAverage[i] < 0){ //As long as the next number exists
    			results += greaterThanAverage[i] + "\n"; //Add each number in array to table
    			i++; //Increase i each time
    		}
    		JOptionPane.showMessageDialog(null, results);
    	}
    
    }
    Last edited by Captain Forge; 11-21-2012 at 08:42 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Java Array Error

    Java Code:
    while (greaterThanAverage[i] >= 0 || greaterThanAverage[i] < 0){
    For starters this condition is redunadant, superfluous, ridiculous etc. It will always be true. Take 10, it is greater than or equal to 0 so true. Take -5, it is less than zero so true. Please provide a value that does not satisfy that condition.

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

    Default Re: Java Array Error

    Quote Originally Posted by Junky View Post
    Java Code:
    while (greaterThanAverage[i] >= 0 || greaterThanAverage[i] < 0){
    For starters this condition is redunadant, superfluous, ridiculous etc. It will always be true. Take 10, it is greater than or equal to 0 so true. Take -5, it is less than zero so true. Please provide a value that does not satisfy that condition.
    I thought the exception was found before this. I knew this would be bad code while typing it (the intention was if there wasn't a value it would throw an error), but I didn't think it could be the cause of this error. I fixed the code and it works fine now. Thank you very much.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Java Array Error

    It causes the error because as I said the condition will always be true. Therefore you will always go into the loop and will always increment i. Eventually i becomes 10 which is an invalid index.

Similar Threads

  1. Array Error
    By crueoj in forum New To Java
    Replies: 3
    Last Post: 07-09-2012, 09:46 PM
  2. Array Size Error
    By pondwire in forum New To Java
    Replies: 5
    Last Post: 07-21-2011, 08:07 AM
  3. Printing 2D Array Error
    By noble in forum New To Java
    Replies: 9
    Last Post: 11-09-2010, 05:31 PM
  4. array error
    By blu_fox88 in forum New To Java
    Replies: 4
    Last Post: 04-25-2010, 12:18 PM
  5. Can't Solve Array Error
    By Gregadeaux in forum New To Java
    Replies: 2
    Last Post: 12-14-2009, 03:01 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
  •