Results 1 to 15 of 15
  1. #1
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Trouble with arrays..

    Ok so here is my problem. I have to write a program where a user can input UP to 25 test scores. The program will then display the scores in a list, then the TOTAL number of test scores that were entered, and then display the highest, lowest, average, etc.

    I have a lot of it done, but now I am having trouble with the following: I have to display the total number of test scores entered and I can't figure out how to do that. Then I have the average done however I need the average to be calculated by the number of scores the user entered, not the total number of scores the user CAN input (i hope that makes sense) Then I can't figure out the above average code.
    Any help would so be greatly appreciated.


    Java Code:
    public class TestData {
    
        // class fields
        private String testName;
        private String input;
        private final int MAXSIZE = 25;
        private int count;
        private int scores[];
    
    
        public TestData() {
    
    	testName = JOptionPane.showInputDialog(null, "Input the name for the test. For example, Test1.");
    	count = 0;
    	scores = new int[MAXSIZE];
    
    	loadArray(scores);
    
      }
    
       private void loadArray(int[] array)  {  // STORE INPUT IN ARRAY
    
          int testsScore = Integer.parseInt(JOptionPane.showInputDialog
                    ("Input test score and press ENTER (-1 to end)"));
        
    
          while(testsScore != -1 && count < MAXSIZE) {
            scores[count] = testsScore;
            count++;
            if(count < MAXSIZE) {
                testsScore = Integer.parseInt(JOptionPane.showInputDialog(
                    "Input test score and press ENTER (-1 to end)"));
            }
          }
        }
    
        public void displayResults()  {  // DISPLAY CONTENTS OF ARRAY AND REST OF OUTPUT
    
    	System.out.println(" The results for " + testName);
    
    	for(int count = 0; count < scores[count]; count++){
       	 	System.out.printf("%5d", scores[count]);
       	 if(count % 10 == 0) {
        	System.out.println();
      }
    }
    	System.out.println(" ");
    	System.out.printf("THE TOTAL NUMBER OF TEST SCORES %d\n", scores[count]);
        System.out.printf("THE NUMBER OF OUTSTANDING SCORES %10d\n", determineOutstanding());
    	System.out.printf("THE NUMBER OF SATISFACTORY SCORES %10d\n", determineSatisfactory());
    	System.out.printf("THE NUMBER OF UNSATISFACTORY SCORES %10d\n", determineUnsatisfactory());
    	System.out.printf("THE AVERAGE SCORES %20f\n", determineAverage());
    	System.out.printf("THE NUMBER OF ABOVE AVERAGE SCORES %10d\n", determineAboveAverage());
    	System.out.printf("THE NUMBER OF BELOW AVERAGE SCORES %10d\n", determineBelowAverage());
    	System.out.printf("THE NUMBER OF HIGHEST SCORES %10d\n", determineHighest());
    	System.out.printf("THE NUMBER OF LOWEST SCORES %10d\n", determineLowest());
    
      }
    
        private int determineOutstanding()  {  // COUNT OUTSTANDING SCORES
          int outstanding = 0;
          for (int i = 0; i < count; i++)
              if(scores[i] >= 90)
                  outstanding++;
          return outstanding;
        }
    
        private int determineSatisfactory()  {  // COUNT SATISFACTORY SCORES
    
    	  int satisfactory = 0;
          for (int i = 0; i < count; i++)
              if(scores[i] >= 60 && scores[i] <= 89)
                  satisfactory++;
          return satisfactory;
        }
    
        private int determineUnsatisfactory()  {  // COUNT UNSATISFACTORY SCORES
    
    	  int unsatisfactory = 0;
          for (int i = 0; i < count; i++)
              if(scores[i] >= 0 && scores[i] <= 59)
                  unsatisfactory++;
          return unsatisfactory;
        }
    
        private double determineAverage()  {  // CALCULATE AVERAGE
    
    	  double total = 0;
    	  double average;
    	  for (int i = 0; i < count; i++)
    	  	total += scores[i];
    	  average = total / scores.length;
    
    	  return average;
        }
    
        private int determineAboveAverage()  {  // COUNT SCORES ABOVE AVERAGE
    
    		double total = 0;
    		double aboveAverage;
    		for (int i = 0; i < count; i++)
    			if(scores[i])
    
          return 0; // so it compiles
        }

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

    Default Re: Trouble with arrays..

    What is the purpose of the count variable?

  3. #3
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    To be honest, I'm not sure. My professor put that in the directions to use. Everything runs fine - it's just the problems I listed. I feel like when the user enters their scores I should be returning some sort of variable that I can use in the get average method and to help list the total number of scores entered. But I cannot think of how to go about that.

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

    Default Re: Trouble with arrays..

    Quote Originally Posted by ertopp View Post
    To be honest, I'm not sure.
    Then I suggest you take a closer look at your code and make sure you understand what it is doing.

  5. #5
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    I understand what it is doing - I just don't know why he assigned the count variable.

    and I can't figure out what variables I would use to calculate above average.

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

    Default Re: Trouble with arrays..

    If you understand what it is doing then you can answer your own question. It is counting how many scores are entered.

  7. #7
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    But when I display the total number of scores entered its outputting 25 which is the MAXSIZE not how many the user actually entered

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

    Default Re: Trouble with arrays..

    Then the loop must be wrong. Put a debugging statement inside the loop to see how many times it gets executed and only input 1 value. If it still iterates 25 times instead of once then it is definately wrong.

  9. #9
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    what is a debugging statement? Sorry, I never heard of that

  10. #10
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    Did you happen to look at my code - this is why I am so confused. I sent my prof. an email asking about the [count] because I'm not sure about it but anyway, my for loop with the scores being inputted works fine then the display method has to list the scores the user inputted and it does that just fine - but then I try to print the total number of scores entered and that comes up as "0"

    I don't understand why that is.

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

    Default Re: Trouble with arrays..

    Use a print statement to output something to the screen. Or have a dialog box popup to display a message.

  12. #12
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    Yes I have the println statement and all of my lines are printing. I guess my question is what variable do I call in order to display the total number of tests scores entered? I have the program displaying the scores individually but then I have to print a line with just the total number of scores the user put it but its coming up as 0

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

    Default Re: Trouble with arrays..

    :headdesk:

    The count variable is counting how many scores are getting entered. But you claim it is 25 regardless of how many scores are entered. So either you are wrong and are not using count correctly or the loop is executing 25 times regardless and count ends up being 25. I have suggested how to test what is actually happening. So now it is up to you to actually debug your code.

  14. #14
    ertopp is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Trouble with arrays..

    I asked how you debug the code

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

    Default Re: Trouble with arrays..

    I told you in reply 11. Add print statements all over your code to see what is getting executed. You can even use them to output the value of variables to see if it matches what you think it should be.

Similar Threads

  1. Trouble with arrays Help
    By mrjaeyun in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 11:14 PM
  2. More trouble with classes and arrays
    By CuddlyKittens11 in forum Advanced Java
    Replies: 9
    Last Post: 04-27-2011, 02:25 AM
  3. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 01:42 AM
  4. Arrays trouble
    By gto400no1 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 02:20 AM
  5. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 02:05 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
  •