I am trying to get a formatted output from 2 arrays one with the countries and the otyher with the populations which should provide an output as follows:
Egypt | 92592000 France | 66991000 Japan | 126860000 Switzerland | 8401120
The only hint I received was that I should calculate the max width required for each column and then use that to align the values. This is what I have come up with so far but stuck on getting anything to output while formatting.
public static void main (String[] args) throws java.lang.Exception
{
String [] countries = {"Egypt", "France", "Japan", "Switzerland"};
int[] populations = {92592000, 66991000, 126860000, 8401120};
printTable(countries, populations);
}
public static void printTable(String[] countries, int[] populations){
int longestInput = 0;
for(int i = 0; i < countries.length; i++){
int countLength = countries[i].length();
int popLength = String.valueOf(populations[i]).length();
if(countLength + popLength > longestInput)
longestInput = countLength + popLength;
}
for(int i = 0; i < countries.length; i++)
System.out.format("%-10",countries[i] + " | " + populations[i]);
}