0

I've written a class and created an override toString() function in my class. I'm trying to get it to print a large list of data, with proper formatting. I've been trying to format it like printf, but it doesn't work properly.

public String toString() {
                return String.format("CRN:" + "%30s", crnNum + "\n" + "Department:" + "%30s", deptCode + "\n" + "Course Number:"
                + "%30s", courseNum + "\n" + "Mode of Instruction:" + "%30s", instructMode + "\n", "Class Days:" + "%30s", meetingDays + 
                "\n" + "Class Times:" + "%30s", meetingTimesStart + "-" + meetingTimesEnd + "\n" + "Class Capacity:" + "%30s", classCapacity +
                "\n" + "Class Enrollment:" + "%30s", classEnrollment + "\n" + "Instructor ID:" + "%30s", instructorID);
}

This properly formats the first line

CRN:    crnNum

with the proper spacing, and goes to a new line, but the second line ends up as

Department:    %30s

and nothing else prints. What am I doing wrong? I've never used String.format before, so I'm not sure if the syntax is totally different and I messed it up, or if it's something obvious I'm missing.

Aaron C.
  • 7
  • 5

0 Answers0