1 2 Previous Next 17 Replies Latest reply: Nov 13, 2007 1:21 AM by 807600 RSS

    Array problem

    807600
      I am trying to add the numbers in the array. if I put in 1,2,3,4,5,6,7 the total should be 28 but for some reason I am getting 21.

      Here is what I have:
      import java.util.Scanner;
      
      public class Diving
      {
           public static void main(String[] args)
           {
                double[] scores = new double[7];
                int posMinScore, posMaxScore;
                double sum = 0;
                double difficulty;
                double finalscore = 0;
                int index = 0;               
                double next = 0;               
                Scanner keyboard = new Scanner (System.in);
                          
                System.out.println("Enter a degree of difficulty between 1.2"
                                                   + " and 3.8: ");
                difficulty = keyboard.nextDouble();
                
                
                System.out.println("Enter 7 scores between 0 and 10: ");
                next = keyboard.nextDouble();               
                posMaxScore = (int)scores[0];          
                posMinScore = (int) scores[0];
                
                
                while ((next >= 0) && (next <= 10) && (index < scores.length - 1)
                          && (difficulty >= 1.2) && (difficulty <= 3.8))
                     {
                          scores[index] = next;
                          index++;
                          next = keyboard.nextDouble();
                               if(scores[index] > posMaxScore)
                
                          posMaxScore = (int)scores[index];
                          
                          if(scores[index] < posMinScore)
                          
                               posMinScore = (int)scores[index];
                               
                          finalscore = finalscore + scores[index];     
                                    
                     }
                for (index = 0; index < scores.length; index++)
                {
                     finalscore = finalscore + scores[index];
                }
                
                 
                
           System.out.println("The diver's final score is " + finalscore);               
           }
           
                
                
      }
        • 1. Re: Array problem
          807600
          ignore the first finalscore =
          • 2. Re: Array problem
            807600
            I found out it is cutting off the last digit but I don't know why?
            • 3. Re: Array problem
              807600
              Have you tried to give <= in the last for loop..
              for (index = 0; index < scores.length; index++)
              {
              finalscore = finalscore + scores[index];
                }
              • 4. Re: Array problem
                807600
                Yes I get an array out of bounds exception.
                • 5. Re: Array problem
                  807600
                  Walk through your code, step by step, and in particular walk through your loop. You are throwing out the last double input into next and not putting it into the array. To see this, do as I suggested above.
                  • 6. Re: Array problem
                    807600
                    when the index becomes 6, the condition in th while loop
                    (index < scores.length - 1)
                    fails and hence it is not picking up the last value of the scores array.

                    Thats the problem. Correct that and your code will work fine.

                    All the best.
                    • 7. Re: Array problem
                      807600
                      I took the - 1 off and I got another out of bounds exception.
                      • 8. Re: Array problem
                        807600
                        Rather than "trying things", read reply 5 and do what it says to see for yourself what the problem is. You read these double values, but you don't add them to the array until the next time around the loop. In the case of the last value you enter that "next time" never happens.
                        • 9. Re: Array problem
                          807600
                          A couple of suggestions:

                          1) All of the scores should be obtained within the loop. You're getting the first score outside of the loop, and that may be messing you up.

                          2) I'd use a for loop to get the 7 scores.

                          3) I'd put the double obtained from the scanner in a temp variable.

                          4) I'd put a while block inside of the for loop and get the double input from the scanner from inside of the while loop. The while loop will repeat until the temp variable is within the proper range (0 - 10).

                          5) After the temp variable is found to be ok, and we're now outside of the while loop, I'd place it into the scores array.

                          in pseudocode:
                          For i goes from 0 to 7
                             while not inputOK
                                temp gets nextdouble from scanner
                                if temp within range then inputOK is set to true
                             end while
                             score array gets temp variable at i position
                             add temp to finalScore
                             check against max and min
                          end For
                          • 10. Re: Array problem
                            807600
                            you are getting that ArrayIndexOutOfBoundsException because,

                            when the index becomes 6, it will go inside the loop but after the line
                            index++
                            the index becomes 7 and
                                 
                                   if(scores[index] > posMaxScore)
                            this line will throw that exception because there is no element in the 7th position of scores array. got it.

                            All the best.
                            • 11. Re: Array problem
                              807600
                              Thank you for your suggestion. I have been on this question for 12+ hours and I just have one thing wrong. Right now I am not so motivated to start doing things over.

                              Thanks again but I will give up on this one. I gave it my best shot.
                              • 12. Re: Array problem
                                807600
                                try this code
                                public static void main(String[] args) {
                                
                                          double[] scores = new double[7];
                                          int posMinScore, posMaxScore;
                                          double sum = 0;
                                          double difficulty;
                                          double finalscore = 0;
                                          int index = 0;               
                                          double next = 0;               
                                          Scanner keyboard = new Scanner (System.in);
                                                    
                                          System.out.println("Enter a degree of difficulty between 1.2"
                                                                             + " and 3.8: ");
                                          difficulty = keyboard.nextDouble();
                                          
                                          
                                          System.out.println("Enter 7 scores between 0 and 10: ");
                                          next = keyboard.nextDouble();          
                                          scores[0]=next;
                                          posMaxScore = (int)scores[0];          
                                          posMinScore = (int) scores[0];
                                          
                                          
                                          while ((next >= 0) && (next <= 10) && (index < scores.length-1)
                                                    && (difficulty >= 1.2) && (difficulty <= 3.8))
                                               {
                                                    index++;
                                                    next = keyboard.nextDouble();
                                                    if(index!=0)
                                                         scores[index] = next;
                                               
                                                    if(scores[index] > posMaxScore)
                                          
                                                    posMaxScore = (int)scores[index];
                                                    
                                                    if(scores[index] < posMinScore)
                                                    
                                                         posMinScore = (int)scores[index];
                                                         
                                                    //finalscore = finalscore + scores[index];     
                                                              
                                               }
                                          for (index = 0; index < scores.length; index++)
                                          {
                                               finalscore = finalscore + scores[index];
                                          }
                                          
                                           
                                          
                                     System.out.println("The diver's final score is " + finalscore);          
                                
                                     }
                                • 13. Re: Array problem
                                  807600
                                  we can guide you, that doesn't mean we have to write your code and give you spoon feed. Please understand that. Next time don't except others to write your code and give.

                                  Have a nice day.

                                  All the best
                                  • 14. Re: Array problem
                                    807600
                                    I will let you know what happens. Thanks. As you can see, I was about to throw in the white towel.
                                    1 2 Previous Next