Home > Back-end >  How to format and print a String[] array and int[][] array into a table together
How to format and print a String[] array and int[][] array into a table together

Time:12-07

import java.util.*;
import java.io.*;

public class GradeBook 
{

    public static void main(String[] args) 
    {
        System.out.println("Starting program\n\n");
        String[] STUDENT_NAMES = new String[] {"Adams", "Baker", "Campbell", "Dewey", "East"};
        int[][] STUDENT_GRADES = new int[5][3];
        
        
        
        for(int i = 0; i<5; i  )
        {
            for(int j = 0; j<3; j  )
            {
                STUDENT_GRADES[i][j] = 5;
            }
        }
        
        printArray(STUDENT_NAMES);
        print2DArray(STUDENT_GRADES);   
        
    } //end main
        
    public static void printArray(String[] arr)
    {
        for(int i = 0; i<arr.length; i  )
        {
            System.out.print(arr[i]);
        }
    }
    
    public static void print2DArray(int arr[][])
    {
        for(int row = 0; row<arr.length; row  )
        {
            for(int col = 0; col<arr[row].length; col  )
            {
                System.out.println(arr[row][col]   " ");
            }
        }

    }
    
} //end class

This assignment for class has me creating a Gradebook for students using a String[] for names, and int[][] for grades. The Gradebook displays the students name with their exam grades in the corresponding row. It specifies 5 names to be loaded in the String[], and the int[][] to be of size [5][3]. The final output of the program should look like this:

Adams 75 75

Baker 100 75

Campbell 84 75

Dewey 80 75

East 50 75

I am having trouble figuring out how to print both String[] and int[][] together, with the names being formatted as shown to the left.

In my program I loaded the int[][] with 5's as a test placeholder. I thought of inputting the specific grades when I declared int[][] as a variable, but since its length is [5][3], I thought the String[] should be in int[i][0]

CodePudding user response:

If I understood you correctly, what is difficult for you is the printing together, and you can do it this way:

public static void printArray(String[] names,int [][] grades)
    {
        for(int i = 0; i<5; i  )
        {
            System.out.println(names[i] " "  Arrays.toString(grades[i]).replace(",", "").replace("[", "").replace("]", "").trim());
        }
    }

the output:

Adams 5 5 5
Baker 5 5 5
Campbell 5 5 5
Dewey 5 5 5
East 5 5 5

CodePudding user response:

To print the names and grades together, you can combine the two loops that you have in your printArray and print2DArray methods. You can then use the System.out.printf method to print the name and grades in the format that you want.

Here is an example of how you could update your main method to print the names and grades together:

public static void main(String[] args) 
{
    System.out.println("Starting program\n\n");
    String[] STUDENT_NAMES = new String[] {"Adams", "Baker", "Campbell", "Dewey", "East"};
    int[][] STUDENT_GRADES = new int[5][3];

    for(int i = 0; i<5; i  )
    {
        for(int j = 0; j<3; j  )
        {
            STUDENT_GRADES[i][j] = 5;
        }
    }
    
    // Loop through the names and grades and print them together
    for (int i = 0; i < STUDENT_NAMES.length; i  ) {
        System.out.printf("%s %d %d %d\n", STUDENT_NAMES[i], STUDENT_GRADES[i][0], STUDENT_GRADES[i][1], STUDENT_GRADES[i][2]);
    }

} //end main


This code will print the name and grades of each student on a separate line, using the System.out.printf method to format the output as shown in your example.

  • Related