• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

Loop to update a Java array that has a random number generator

Trying to code a loop that will sum the randomly generated values in my rows (a row Totals) and columns (column Final Totals). The array is 4 X 20, so 80 elements. Not been successful figuring it out. Can someone please help me out as quickly as possible?

I would appreciate the assistance as expeditiously as possible.

--------------------------------------HERE IS THE PROGRAM RUN------------------------------

Enter the number of the game you want to update:  5
The game that will be updated is: 5

                              COLLEGE FOOTBALL TEAM GAME SCORES

__________________________________________________________
Games                                            Quarters                              Totals
_______________1________2________3________4_______________

1                           0               0               0               0               0              
2                           0               0               0               0               0              
3                           0               0               0               0               0              
4                           0               0               0               0               0              
5                           16               2               18               29               0              
6                           0               0               0               0               0              
7                           0               0               0               0               0              
8                           0               0               0               0               0              
9                           0               0               0               0               0              
10                           0               0               0               0               0              
11                           0               0               0               0               0              
12                           0               0               0               0               0              
13                           0               0               0               0               0              
14                           0               0               0               0               0              
15                           0               0               0               0               0              
16                           0               0               0               0               0              
17                           0               0               0               0               0              
18                           0               0               0               0               0              
19                           0               0               0               0               0              
20                           0               0               0               0               0              
__________________________________________________________
Final Totals
__________________________________________________________

--------------------------------------HERE IS THE PROGRAM ---------------------------------
// import java packages
import java.lang.Math; //use to avoid generating negative numbers
import java.util.*;
import java.io.*;
import javax.swing.*;

// classname
public class FootballScoresArray3
{
      public static void main(String[] args) throws IOException
      {
      //hard code constants and declare variables
      int userInputGameNumber = 0; // number of the game  user enters into keyboard
      int QUARTERS_4 = 5;  //4 quarters per game
      int GAMES_20 = 20;  //20 games per season
      int oneGameQuartersTotal = 0;//Total=1 game 4 quarter results
      int allGameQuartersFinalTotal = 0;//Final Totals=20 games results
      double average = 0; // computes average of the scores
      String Totals;
      String Games;
      String Final_Totals;
      
      //parameterless constructor creates instance of random class
      Random generator = new Random();
      
      //user inputs game number      
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter the number of the game you want to update:  ");
      userInputGameNumber = keyboard.nextInt();
      System.out.println("The game that will be updated is: " + userInputGameNumber);
      System.out.println();
      if (userInputGameNumber > 0) //start loop
      {
            // create array header
      System.out.println("                        \tCOLLEGE FOOTBALL TEAM GAME SCORES");
      System.out.println();
      System.out.println("__________________________________________________________ ");      
      System.out.println("Games                                            Quarters                              Totals");
      System.out.println("_______________1________2________3________4_______________ ");      
      System.out.println();
      
            //update array with user input
      int [][] scores = new int[GAMES_20][QUARTERS_4];
      for(int g = 0; g < scores.length; g++)
      {
       for(int q = 0; q < scores[g].length; q++)
       {
        System.out.print((g + 1)             +                   "                           "); //g+1=columns at 1
        for (q = 0; q < scores[g].length; q++)
        System.out.print(scores[g][q]             +                   "               ");
                
        // create array footer      
        System.out.println(); //IMPORTANT--THIS PUTS INFO IN THE TOTALS COLUMN
          
        //get a valid integer for random number generator
      scores[userInputGameNumber - 1][0]= Math.abs (generator.nextInt() % 33);
      scores[userInputGameNumber - 1][1]= Math.abs (generator.nextInt() % 33);
       scores[userInputGameNumber - 1][2]= Math.abs (generator.nextInt() % 33);
       scores[userInputGameNumber - 1][3]= Math.abs (generator.nextInt() % 33);
                           
        
        } //don't delete
      } //don't delete
      } //don't delete                          
        
      System.out.println("__________________________________________________________ ");
      System.out.println("Final Totals");
      System.out.println("__________________________________________________________ ");
      System.out.println();
      
            }
      }
            

0
computerese
Asked:
computerese
  • 2
  • 2
1 Solution
 
smkernCommented:
I have made slight changes, which should demonstrate how to sum up the columns.  I also removed the redundant loop from your code:


// import java packages
import java.lang.Math; //use to avoid generating negative numbers
import java.util.*;
import java.io.*;
import javax.swing.*;

// classname
public class FootballScoresArray3
{
     public static void main(String[] args) throws IOException
     {
     //hard code constants and declare variables
     int userInputGameNumber = 0; // number of the game  user enters into keyboard
     int QUARTERS_4 = 5;  //4 quarters per game
     int GAMES_20 = 20;  //20 games per season
     int oneGameQuartersTotal = 0;//Total=1 game 4 quarter results
     int allGameQuartersFinalTotal = 0;//Final Totals=20 games results
     double average = 0; // computes average of the scores
     String Totals;
     String Games;
     String Final_Totals;
     //New variable
     int[] sums = new int[QUARTERS_4 ];
     for(int i=0;i<sums.length; i++)
     {
           sums[i] = 0;
     }

     //parameterless constructor creates instance of random class
     Random generator = new Random();

     //user inputs game number
     Scanner keyboard = new Scanner(System.in);
     System.out.print("Enter the number of the game you want to update:  ");
     userInputGameNumber = keyboard.nextInt();
     System.out.println("The game that will be updated is: " + userInputGameNumber);
     System.out.println();
     if (userInputGameNumber > 0) //start loop
     {
          // create array header
     System.out.println("                    \tCOLLEGE FOOTBALL TEAM GAME SCORES");
     System.out.println();
     System.out.println("__________________________________________________________ ");
     System.out.println("Games                                     Quarters                         Totals");
     System.out.println("_______________1________2________3________4_______________ ");
     System.out.println();

          //update array with user input
     int [][] scores = new int[GAMES_20][QUARTERS_4];
     for(int g = 0; g < scores.length; g++)
     {
        //moved outside of loop
        System.out.print((g + 1)           +                "                       "); //g+1=columns at 1
      for(int q = 0; q < scores[g].length; q++)
      {
       //Removed unnecesary loop.

       //get a valid integer for random number generator
     scores[userInputGameNumber - 1][0]= Math.abs (generator.nextInt() % 33);
     scores[userInputGameNumber - 1][1]= Math.abs (generator.nextInt() % 33);
      scores[userInputGameNumber - 1][2]= Math.abs (generator.nextInt() % 33);
      scores[userInputGameNumber - 1][3]= Math.abs (generator.nextInt() % 33);

        //To sum a row
        scores[userInputGameNumber - 1][4] = scores[userInputGameNumber - 1][0] + scores[userInputGameNumber - 1][1] + scores[userInputGameNumber - 1][2] + scores[userInputGameNumber - 1][3];

        //To sum the columns:
        sums[q] += scores[g][q];

       //Moved to after the values are set
       System.out.print(scores[g][q]           +                "             ");

       } //don't delete
       //move outside of loop
       // create array footer
       System.out.println(); //IMPORTANT--THIS PUTS INFO IN THE TOTALS COLUMN
     } //don't delete
     } //don't delete

     System.out.println("__________________________________________________________ ");
     System.out.println("Final Totals");
       //To display totals:
        for(int i=0;i<sums.length; i++)
       {
             System.out.print(String.valueOf(sums[i]) + "\t");
       }
       System.out.println();
     System.out.println("__________________________________________________________ ");
     System.out.println();

          }
     }
0
 
computereseAuthor Commented:
@smkern

Thanks for your fast and useful response. I'm still at work now so I won't have a chance to incorporate your changes for a few hours. I hope you are still checking this area in 3 or 4 hours; I might have 1 or 2 more questions and I'm very impressed and pleased with your expertise.

Sincerely,
0
 
computereseAuthor Commented:
@smkern:

I just incorporated your suggestions but I'm not getting a total. Values are being put in the row column from the random generator, but not a sum. Can you help? Ditto for the column totals.
0
 
smkernCommented:
As far as I could see, the only problem was an incorrect column total.  This version should fix that(Moved calculation into just the row loop)
Thiese lines should print the totals at the bottom:(since there is only 1 non-zero value, it will be whatever row is set above)
   //To display totals:
       for(int i=0;i<sums.length; i++)
      {
           System.out.print(String.valueOf(sums[i]) + "\t");
      }

//Slightly modified version

// import java packages
import java.lang.Math; //use to avoid generating negative numbers
import java.util.*;
import java.io.*;
import javax.swing.*;

// classname
public class FootballScoresArray3
{
     public static void main(String[] args) throws IOException
     {
     //hard code constants and declare variables
     int userInputGameNumber = 0; // number of the game  user enters into keyboard
     int QUARTERS_4 = 5;  //4 quarters per game
     int GAMES_20 = 20;  //20 games per season
     int oneGameQuartersTotal = 0;//Total=1 game 4 quarter results
     int allGameQuartersFinalTotal = 0;//Final Totals=20 games results
     double average = 0; // computes average of the scores
     String Totals;
     String Games;
     String Final_Totals;
     //New variable
     int[] sums = new int[QUARTERS_4 ];
     for(int i=0;i<sums.length; i++)
     {
          sums[i] = 0;
     }

     //parameterless constructor creates instance of random class
     Random generator = new Random();

     //user inputs game number
     Scanner keyboard = new Scanner(System.in);
     System.out.print("Enter the number of the game you want to update:  ");
     userInputGameNumber = keyboard.nextInt();
     System.out.println("The game that will be updated is: " + userInputGameNumber);
     System.out.println();
     if (userInputGameNumber > 0) //start loop
     {
          // create array header
     System.out.println("                    \tCOLLEGE FOOTBALL TEAM GAME SCORES");
     System.out.println();
     System.out.println("__________________________________________________________ ");
     System.out.println("Games                                     Quarters                         Totals");
     System.out.println("_______________1________2________3________4_______________ ");
     System.out.println();

          //update array with user input
     int [][] scores = new int[GAMES_20][QUARTERS_4];
     for(int g = 0; g < scores.length; g++)
     {
       //moved outside of loop
       System.out.print((g + 1)           +                "                       "); //g+1=columns at 1
       //get a valid integer for random number generator
         scores[userInputGameNumber - 1][0]= Math.abs (generator.nextInt() % 33);
         scores[userInputGameNumber - 1][1]= Math.abs (generator.nextInt() % 33);
         scores[userInputGameNumber - 1][2]= Math.abs (generator.nextInt() % 33);
         scores[userInputGameNumber - 1][3]= Math.abs (generator.nextInt() % 33);

                //To sum a row
       scores[userInputGameNumber - 1][4] = scores[userInputGameNumber - 1][0] + scores[userInputGameNumber - 1][1] + scores[userInputGameNumber - 1][2] + scores[userInputGameNumber - 1][3];
      for(int q = 0; q < scores[g].length; q++)
      {
       //Removed unnecesary loop

       //To sum the columns:
       sums[q] += scores[g][q];

       //Moved to after the values are set
       System.out.print(scores[g][q]           +                "             ");

       } //don't delete
       //move outside of loop
       // create array footer
       System.out.println(); //IMPORTANT--THIS PUTS INFO IN THE TOTALS COLUMN
     } //don't delete
     } //don't delete

     System.out.println("__________________________________________________________ ");
     System.out.println("Final Totals");
      //To display totals:
       for(int i=0;i<sums.length; i++)
      {
           System.out.print(String.valueOf(sums[i]) + "\t");
      }
      System.out.println();
     System.out.println("__________________________________________________________ ");
     System.out.println();

          }
     }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now