Condensing multiple System.out.prinln() into one [closed]

I have

        for( int x = 0; x < rp.studentRecords.length; x++){
        System.out.println("Student name: " +rp.studentRecords[x].getName());
        System.out.println("Major: "+ rp.studentRecords[x].getClass().getName());
        System.out.println("Age: "+ rp.studentRecords[x].getName());
        System.out.println("Year " +rp.studentRecords[x].getYear());
        System.out.println("Credits Earned: "+ rp.studentRecords[x].getCredits());
        System.out.println("Remaining Credits: "+ rp.studentRecords[x].getRemainingCredits());
        System.out.println("-------");
    }

and I need to condense those System.out.println()s into a single one.

Answer

What you could try is to use String.format like

String output = String.format ("Name: %snMajor: %snAge: %s", 
  rp.studentRecords[x].getName(),
  rp.studentRecords[x].getClass().getName(),
  rp.studentRecords[x].getAge());

// then print it out
System.out.println (output);

Leave a Reply

Your email address will not be published. Required fields are marked *