Results 1 to 3 of 3

Thread: Array problem

  1. #1
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Array problem

    Hello,
    I am trying to write code to store integers (scores), print them out, and then display the number of scores entered. However, if you enter 3 integers it displays the 3 you entered, 2 zero's, and then tells you you entered 4 scores?! Im very confused here and have tried various if statements, loops, decrementors, etc and I am still at a loss for a solution.

    Any help would be appreciated!!

    Java Code:
    import javax.swing.*;
    
    public class Scores {
    
      private final static int SIZE = 25;
    
    
      public static void main(String[] args)  {
    
        int [] scores = new int[SIZE];
        int count = 0;
        double average = 0.0;
    
        count = loadArray(scores);
        average = determineAverage(scores, count);
        displayResults(scores, count, average);
        System.exit(0);
      }
    
    
    //returns the count of the scores entered
      private static int loadArray(int[]n)  {
      	int s = 0;
      	int i;
    s = Integer.parseInt(JOptionPane.showInputDialog(null, "Input test score"));
    if(s == -1){
    	break ;
    } else {
    	n[i] = s;
    	i++;
    }
    
    	return(i+1);  // so file compiles
      }
    
    
    //displays all of the results
     private static void displayResults(int[] n, int cnt, double ave)  {
    
    
    
      	for(int i = 0; i<=cnt; i++){
    
      		System.out.println(n[i]);
      		if(n[i]%10 == 0){
      			System.out.println();
      		}
      	}
    
    	System.out.println("The total number of test scores" + cnt);
    //	int outstanding = determineOutstanding(0);
    	//System.out.println("The number of Outstanding Scores" + outstanding);
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    variable int count is not an array

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    And also if this is the complete class you've written, seems to me that you've not compile the code.

    1. Closing bracket of the class is missing.

    2. I can't see the method determineAverage is implemented in your class.

    3. In loadArray I can't see that variable i is not initialize before use.

    Don't you notice those things?

Similar Threads

  1. Array problem please help
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 12-15-2010, 01:38 PM
  2. Problem with Array Use
    By Mike90 in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 03:45 PM
  3. Array problem
    By c3jcarmy in forum New To Java
    Replies: 11
    Last Post: 03-11-2010, 03:45 AM
  4. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 03:25 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:13 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
  •