So, I've been frollicking around with this for a while and encountered things like using Format and /t and what not. But I still cant figure out how to properly outline the output of my println made by this for loop:
for (int i = 0; i < AANTAL_CIJFERS; i++) //Toon cijfers per vak
{
System.out.println("Vak/project:\t" + vakken[i] + "\tCijfer\t: " + cijfers[i] + "\tBehaalde punten: " + puntBehaald(i));
}
It currently prints like this:
But i'd like the printline to look more like this:
But I just cant figure it out, anybody know how I could accomplish this?
On this case you have to use java System.out.printf()
or String.format()
methods instead of System.out.print()
or System.out.println()
Please read this exmples how-to-use-formatting-with-printf-correctly-in-java and tabs-does-not-result-in-aligned-columns for more details.
Also here is a helpful article java-string-format-examples
I just update the code to be as the following and it works fine with me :
private static String format = "%s %-30s ";
private static String format2 = "%s %3s ";
.
.
for (int i = 0; i < AANTAL_CIJFERS; i++) //Toon cijfers per vak
{
System.out.printf(format, "Vak/project: ", vakken[i]);
System.out.printf(format2, "Cijfer: ", cijfers[i]);
System.out.printf(format2, "Behaalde punten: ", puntBehaald(i));
System.out.printf("%n");
}
So I am now using this as code:
for (int i = 0; i < AANTAL_CIJFERS; i++) //Toon cijfers per vak
{
System.out.printf(format, "Vak/project: ", vakken[i]);
System.out.printf(format, "Cijfer: ", cijfers[i]);
System.out.printf(format, "Behaalde punten: ", puntBehaald(i));
System.out.printf("\n");
}
So it clearly doesnt work the way I'd expect it to from the documentation. What am I doing wrong? It should be: Vak/project: "output from vakken[i]" TAB Cijfer: "output from cijfers[i]" TAB Behaalde punten: "output from puntBehaald(i)".
This is my format right now: private static String format = "%-20s%s";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.