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];
loadGradeArray(STUDENT_GRADES);
for (int i = 0; i < STUDENT_NAMES.length; i )
{
System.out.printf("%s %d %d \n", STUDENT_NAMES[i], STUDENT_GRADES[i][0], STUDENT_GRADES[i][1]);
}
} //end main
public static void loadGradeArray(int[][] STUDENT_GRADES)
{
for(int row = 0; row<STUDENT_GRADES.length; row )
{
for(int col = 0; col<STUDENT_GRADES[row].length; col )
{
STUDENT_GRADES[0][0] = 75;
STUDENT_GRADES[0][1] = 75;
STUDENT_GRADES[1][0] = 100;
STUDENT_GRADES[1][1] = 75;
STUDENT_GRADES[2][0] = 84;
STUDENT_GRADES[2][1] = 75;
STUDENT_GRADES[3][0] = 80;
STUDENT_GRADES[3][1] = 75;
STUDENT_GRADES[4][0] = 50;
STUDENT_GRADES[4][1] = 75;
}
}
}
}
Assignment for class has me creating a grade book for five students with two exam grades listed by their name. The finished product gives the user the option of either printing the average grade for a student, or printing the class average for a test
I am having trouble with formatting the spacing between the values in the grade book. I want the numbers to be evenly aligned-right, like this:
Adams _______75 75
Baker _______100 75
Campbell _____84 75
Dewey _______80 75
East _________50 75
Ignore the '_'. Not sure how to format that
CodePudding user response:
Use the below code to print the data
System.out.printf("%-10s %d %d \n", STUDENT_NAMES[i], STUDENT_GRADES[i][0], STUDENT_GRADES[i][1]);
Check the full code and output here
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];
loadGradeArray(STUDENT_GRADES);
for (int i = 0; i < STUDENT_NAMES.length; i )
{
System.out.printf("%-10s %d %d \n", STUDENT_NAMES[i], STUDENT_GRADES[i][0], STUDENT_GRADES[i][1]);
}
} //end main
public static void loadGradeArray(int[][] STUDENT_GRADES)
{
for(int row = 0; row<STUDENT_GRADES.length; row )
{
for(int col = 0; col<STUDENT_GRADES[row].length; col )
{
STUDENT_GRADES[0][0] = 75;
STUDENT_GRADES[0][1] = 75;
STUDENT_GRADES[1][0] = 100;
STUDENT_GRADES[1][1] = 75;
STUDENT_GRADES[2][0] = 84;
STUDENT_GRADES[2][1] = 75;
STUDENT_GRADES[3][0] = 80;
STUDENT_GRADES[3][1] = 75;
STUDENT_GRADES[4][0] = 50;
STUDENT_GRADES[4][1] = 75;
}
}
}
}
Output
Adams 75 75
Baker 100 75
Campbell 84 75
Dewey 80 75
East 50 75
CodePudding user response:
Something like this will keep everything in a nice, even, columnar format:
String[] STUDENT_NAMES = new String[]{"Adams", "Baker", "Campbell", "Dewey", "East"};
int[][] STUDENT_GRADES = new int[5][3];
loadGradeArray(STUDENT_GRADES);
String header = String.format("%-10s %7s %8s", "Name", "Class-1", "Class-2");
String underline = String.join("", java.util.Collections.nCopies(header.length(), "-"));
System.out.println(underline);
System.out.println(header);
System.out.println(underline);
for (int i = 0; i < STUDENT_NAMES.length; i ) {
System.out.printf("%-10s ]