hi, im tring to print this but with proper spaces, to make it more visual:
System.out.printf("%-25s %-25s %-20s %-21s %-21s %-24s %-24s %-25s %-25s %-20s %-20s %-20s %-25s %n", "{ nom: " + nom , "tipus: " + tipusS , "número: " + numeroS , "valor: " + valorS , "pedres: " + pedresS , "tirada: " + tiradaS , "agafada: " + agafadaS , "triomf: " + triomfS , "última: " + ultimaS , "tirada per: " + tiradaPerS , "recollida per: " + recollidaPerS , "tirada primer: " + tiradaPrimerS , " }");
but as u see, is not really good, like, sometimes there is more spaces, and sometimes less, and finally, there isnt
how could i do it better?
(i tried to put 20s to each one, but looks worse)
So this is actually something that really old programs needed to do a lot. And honestly most bank statements, etc.
But it's weirdly not super supported in modern languages. If you don't care about how long it gets we can make a method that prints to a table so.
public static void outputAsTable(List<Map<String, String>> stuff) {
}
Start with this - assume we have a list of maps you want to print out. We can scan through each map and find the longest value for each key.
public static void outputAsTable(List<Map<String, String>> stuff) {
Map<String, Integer> longestValues = new HashMap<>();
for (var map : stuff) {
for (var entry : map) {
if (longestValues.getOrDefault(entry.getKey(), 0) < entry.getValue().length()) {
.put(entry.getKey(), entry.getValue().length()
longestValues}
}
}
...
}
Then once you have all those you can do math to figure out how many spaces to add.
I think this will be hard, I've never worked with this maps, and this variables are not stored inside any array, they are just the attributes of a class, so I print them to check how all is working
You just need to convert your objects to a list of maps, unfortunately. If you want it all properly spaced you need to print them all at once not individually since if you think about it, how much padding to give any one element can be determined by what you want to print 10 items later.
Another option is to print vertically
field_1: ...
field_abc: ...
ww: ...
-------------------
field_1: ...
field_abc: ...
ww: ...