Home > other >  Taking Scanner Data and transposing into Array (Java)
Taking Scanner Data and transposing into Array (Java)

Time:03-06

Got this assignment with the purpose to withdraw a minimum, maximum, and average score from various user inputs. I think that I've got the loop for the data transfer down, thanks to people who commented on the first draft of this post.

public class BattingAverage
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        // Declare a named constant for array size here.
        int arraySize = 4;

        // Declare array here.
        double[] averages = new double[arraySize];

        // Use this integer variable as your loop index.
        int loopIndex;

        // Use this variable to store the batting average input by user.
        double battingAverage = 0;

        // String version of batting average input by user.
        String averageString; 

        // Use these variables to store the minimim and maximum batting averages.
        double min, max;

        // Use these variables to store the total and the average.
        double total = 0;
        double average = 0; 

        // Write a loop to get batting averages from user and assign to array.
        
           System.out.println("Enter a batting average: ");
        
           averageString  = s.nextLine();
           battingAverage = Double.parseDouble(averageString);
           for (double x = 0; x < arraySize; x  ) {
               averages[x] = s.nextDouble();
               
           }
          
        // Assign the first element in the array to be the minimum and the maximum.
        min = averages[0];
        max = averages[0];
        // Start out your total with the value of the first element in the array.
        total = averages[0];  
        for(Double avgValue:avg) {
            total = averages[x];
        }
        // Write a loop here to access array values starting with averages[1]
        
           // Within the loop test for minimum and maximum batting averages.
           
           // Also accumulate a total of all batting averages.
           
        
           
        // Calculate the average of the 8 averages.
        

        // Print the averages stored in the averages array. 
        
        // Print the maximum batting average, minimum batting average, and average batting average. 
        
        System.exit(0);

    }
}

Fixed the loop to properly take user input and place it in the array, but now im stumped on assignment of the min, max, and total.

CodePudding user response:

On this line:

battingAverage = Scanner.nextDouble();

When you refer to Scanner, you are referring to the class Scanner and all of the static methods on it. nextDouble() is a member level method of Scanner similar to nextLine(); it can only be called on an instance of class Scanner. If you change it to s.nextDouble() that compilation error should go away.

CodePudding user response:

When you refer to the indexes of an array, the value used as the index doesn't have to be a constant.

So you can, for example, say:

int x = 1;
avg[x] = someNumber;

Does that give you any ideas for simplifying the code inside your for loop?

CodePudding user response:

Try to understand what problem is and what you need to do. Using all available variable is not at all required you just need to take input put those input in array and then after to do all calculation like total, min, max, average.

Also while taking input in Array you don't required to provide index manually like avg[0],avg[1] you are running a loop use that index variable . Please find the code snippest below

public class BattingAverage
{
    public static void main(String args[])
    {
      
    Scanner s = new Scanner(System.in);
                // named constant for array size here
                int arraySize = 4;
        
                //declaring array where we will store all batting average input
                double[] avg = new double[arraySize];
        
                // Use this integer variable as your loop index. (Given in the assignment)
                int loopIndex;
        
                // Use this variable to store the batting average input by user. (Also Given)
                double battingAverage = 0;
        
                // String version of batting average input by user. (Given)
                String averageString;
        
                // Use these variables to store the minimum and maximum batting averages. (Given)
                double min, max;
        
                // Use these variables to store the total and the average. (Given)
                double total=0,average=0;
        
                // I wrote these statements to get user input and then store it in the "avg" array
        
                System.out.println("Enter a batting average: ");
                for (int x = 0; x<arraySize; x  ) {
                    avg[x] = s.nextDouble(); //storing all input to array
                    //replaced Scanner with s explnation can be seen in Scott jordan comment above
        
                }
                min=avg[0]; // intialy considering first element is smallest  will update if smaller than this found
                max=avg[0]; //intialy considering first element is smallest will update if larger  than this found
                for(Double avgVal:avg)
                {
                    total =avgVal; //adding it to total
                    if(avgVal>max) // checking current value greater than , if greater then change max to current value
                    {
                        max=avgVal;
                    }
                    if(avgVal<min)// checking current value less than , if less then change min to current value
                    {
                        min=avgVal;
                    }
                }
                average=total/arraySize; //divinf total by total number of element to get average
        
                System.out.println("min= =" min " max= " max " total= " total " average= " average);

}
}
  • Related