I'm Learning Java and I thought I maybe create a simple table using arrays. Not sure if the word "Table" really refers to another Java topic that I'm not aware yet but be patient with me. I'm trying to make something like this
Country Capital
Mexico DF
Guatemala Guatemala City
El Salvador El Salvador
Russia Moscow
Took me some time to figure how to loop in the arrays and get the values but I'm not really able to display the format above. You the Experts have a good idea for this newbie?
I appreciate your patience and help.
public class Arrays39 {
public static void main(String[] args) throws Exception {
String[][] countries = new String[4][2];
countries[0][0] = "Mexico";
countries[0][1] = "DF";
countries[1][0] = "Guatemala";
countries[1][1] = "Guatemala City";
countries[2][0] = "EL Salvador";
countries[2][1] = "El Salvador";
countries[3][0] = "Rusia";
countries[3][1] = "Moscow";
System.out.println("Country " "Capital");
for (int i = 0; i < countries.length; i ) {
for (int j = 0; j < 2; j ) {
System.out.println(countries[i][j]);
}
}
}
}
CodePudding user response:
This is one way I thought of to create a table manually. One thing to consider is that you can't separate both columns evenly without considering word lengths. You can create a solution for this problem by considering the largest word length in the first column and generate a padding in between with it. Then you have to consider words in the left column that are smaller in length, so you add the extra padding by considering: max word length - current word length. If you have any questions, please feel free to ask.
public class Arrays39 {
public static void main(String[] args) {
String[][] countries = {
{"Mexico" , "DF"},
{"Guatemala" , "Guatemala City"},
{"El Salvador" , "El Salvador"},
{"Russia" , "Moscow"},
};
generateTable(countries, maxPadding(countries));
}
/* Prints out 2d-array */
public static void generateTable(String[][] countries, int padding) {
System.out.printf("Country %s Capital\n", addPadding(" ", padding));
for(String[] country : countries) {
System.out.printf("%s %s %s%n", country[0], addPadding(country[0], padding), country[1]);
}
}
/* Two helper methods that find the max padding and the other calculates current word length to max padding */
/* Generates padding from the country with the longest name */
private static int maxPadding(String[][] s) {
int max = 0;
for(int i = 0; i < s.length; i ) {
if(s[i][0].length() > max) max = s[i][0].length();
}
return max;
}
/* Formats each country name's padding based off the result of generatePadding() */
private static String addPadding(String country, int padding) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < padding - country.length(); i ) {
sb.append(" ");
}
return sb.toString();
}
}
Output:
Country Capital
Mexico DF
Guatemala Guatemala City
EL Salvador El Salvador
Rusia Moscow
CodePudding user response:
Use method printf rather than method println
or method print
.
In order to display as a table, you need to first know the longest country name. In the below code, the first for
loop does that.
public class Arrays39 {
public static void main(String[] args) throws Exception {
String[][] countries = new String[4][2];
countries[0][0] = "Mexico";
countries[0][1] = "DF";
countries[1][0] = "Guatemala";
countries[1][1] = "Guatemala City";
countries[2][0] = "EL Salvador";
countries[2][1] = "El Salvador";
countries[3][0] = "Rusia";
countries[3][1] = "Moscow";
int max = 0;
for (int i = 0; i < countries.length; i ) {
int len = countries[i][0].length();
if (len > max) {
max = len;
}
}
System.out.printf("%-" max "s Capital%n", "Country");
for (int i = 0; i < countries.length; i ) {
System.out.printf("%-" max "s %s%n", countries[i][0], countries[i][1]);
}
}
}
Running above code produces following output:
Country Capital
Mexico DF
Guatemala Guatemala City
EL Salvador El Salvador
Rusia Moscow