This program left-justifies color names and right-justifies a parallel array of ints. We do this with just one format string.
public class Program {
public static void main(String[] args) {
String[] colors = {
"red",
"orange",
"blue",
"green" };
int[] numbers = { 42, 100, 200, 90 };
// Loop over both arrays.
for (int i = 0; i < colors.length; i++) {
// Left-justify the color string.
// ... Right-justify the number.
String line = String
.format(
"%1$-10s %2$10d", colors[i], numbers[i]);
System.out.println(line);
}
}
}
red 42
orange 100
blue 200
green 90