4 Replies Latest reply: Feb 27, 2008 1:51 PM by 807603 RSS

    creating array via scanner

    807603
      My task is to create an array with 10 elements, the elements represent grades for students on a scale of 1-10. I can't figure out how to get the object from the scanner to the array. Any ideas?

      the following is the code I have so far...
      import java.util.Scanner;
      
      public class Array
      {
      
           //Chad Scribner
           //COS 160-Spring 2008 Professor Bantz
           //Assignment #5
           //File: Array.java
           
           public static void main(String[] args)
           {          
                //Scanner and variable initialization
                Scanner console = new Scanner(System.in);
                System.out.println("Please enter grades.");
                int input = console.nextInt();
                int[] grades = new int[10];
                int sum = 0;
                for (int i = 1; i <= input; i++)
                {
                     grades[i] = console.nextInt();
                     sum += grades;
                }
                //Calculation output
                System.out.println(grades);
           }
      }
      Edited by: Jago6060 on Feb 26, 2008 7:42 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: creating array via scanner
          807603
          There is something unclear about the question. You say:
          System.out.println("Please enter grades.");
          int input = console.nextInt();
          int[] grades = new int[10];
          int sum = 0;
          for (int i = 1; i <= input; i++)
          {
          The loop suggests that input is the number of grades. But your question (and the grades array) suggest that there should be exactly 10 grades entered. Which is it?

          The general approach you take - reading the input, assigning it to an array element and incrementing the array index - is OK. If it is not doing what you expect or intend perhaps you could say what that is, and what it is, in fact, doing.
          • 2. Re: creating array via scanner
            807603
            import java.util.Scanner;
             
            public class Array
            {
             
                 //Chad Scribner
                 //COS 160-Spring 2008 Professor Bantz
                 //Assignment #5
                 //File: Array.java
                 
                 public static void main(String[] args)
                 {          
                      //Scanner and variable initialization
                      Scanner console = new Scanner(System.in);
                                            int[] grades = new int[10];
                                            int sum = 0;
            
                                            for(int i = 0; i < grades.length; i++) {
                      System.out.println("Please enter grades.");
                      int input = console.nextInt();
                      grades[i] = input;
                                           System.out.println(grades);
                      sum += grades;
                      }
                      //Calculation output
            System.out.println("Total sum =" + sum);

                           }
            }
            I'm not sure if i get your question correctly. My suggested codes reapeatedly ask you for 10 integer grade inputs. Upon entering the 10th grade, a total sum will be displayed. Is this what you are looking for?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            • 3. Re: creating array via scanner
              807603
              I apologize for being unclear in my question. My end goal is to display a histogram using asterisks to indicate the number of times a given grade in the array appears. What I'm trying to do at the moment is get random grades(entered by the user) into the array and display the array. The max number of grades in the array is to be 10, grades range from 1-10, after the 10 grades are entered, I want the array to be displayed.

              Just to give an example of the end historgram...

              1 *
              2 ****
              3 ***
              4
              5 **
              etc.

              Edited by: Jago6060 on Feb 27, 2008 1:09 PM
              • 4. Re: creating array via scanner
                807603
                The max number of grades in the array is to be 10, grades range from 1-10, after the 10 grades are entered, I want the array to be displayed.
                In that case change the variable you call input to something like numGrades so that its name reflects its function.

                Take out the sum variable as its not being used yet.

                It is very common for for loop indices to start at zero rather than 1. Since you are assigning to an array this makes even more sense as array indices also start at zero. So the for loop should be something like
                for (int i = 0; i <= numGrades; i++)
                {
                    grades[i] = console.nextInt();
                }
                Use another (similar) for loop to print the grades array to check that the output matches your expectations.