2

First, I apologize if something like this has been answered already. I tried to search for any questions like this but I didn't find anything similar (at least with the keywords I used). Please moderate this as needed! It's my first post on the site.

I'm practicing String.format in Java and having trouble with making the elements of my 'Name' column all the same width. I'm using JDK 9 if that's relevant at all. In my code below, the issue in question is within the for-loop block. I'm taking the first and last names from their respective String arrays. I know I can solve my problem if I made each full name into one String, but for the sake of this exercise and in order to further my Java skills, I'd like to know if there is a solution to this problem. Thank you :)

public static void grades() {
    Random rand = new Random();
    // final grade is calculated based on points achieved out of 45. Rounded to 1 decimal
    String[] rosterFirst = {"Harry", "Seamus", "Dean", "Neville", "Ron", "Dennis"};
    String[] rosterLast = {"Potter", "Finnegan", "Thomas", "Longbottom", "Weasley", "Creevey"};

    System.out.println("2018 Hogwarts Graduating Class Scores \n" +
        String.format("%2$-25s %1$-12s", "Grade", "Name"));

    for (int i = 0; i < rosterFirst.length; i++) {
        double finalGrade = (25 + rand.nextDouble() * 20) / 45 * 100;
        System.out.println(String.format("%s, %-15s %-12.1f", rosterLast[i], rosterFirst[i], finalGrade));
    }
}

Output:

2018 Hogwarts Graduating Class Scores 
Name                      Grade       
Potter, Harry         70.3
Finnegan, Seamus         75.9
Thomas, Dean         96.9
Longbottom, Neville         98.2
Weasley, Ron         58.7
Creevey, Dennis         76.0

(Not sure if code output is wrongly formatted in this post. Please correct me if it is)

0

1 Answer 1

3

The current problem is that the last name (first argument) does not have a fixed width. This will "mess up" the remainder of the string as the width of the last name changes. Furthermore, it's not possible to specify an absolute position of additional parameters with String.format alone; width formatting only affects relative positions.

An easy way is to get the desired behavior is to specify the full name as a single field with a fixed width, ie:

String fullName = rosterLast[i] + ", " + rosterFirst[i];

System.out.println(
  String.format("%-20s %-12.1f", fullName, finalGrade));

There are now only two arguments actually supplied to the format and both have a fixed width specified. Having the comma be part of the full name avoids fixed-width interactions between the individual name components and the comma.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.