how do I remove all the spaces without changing the format and what is the best option to do so and if so would I be using the System.out.printf(); or some other directive. please respond with java and not C++. I would really appreciate anyone who helps and will gladly thank them for helping me with my own problems.
public class WorldPopulation{
//main string
public static void main(String[] args) {
//prints title
System.out.println(" This is a estimate of world population growth within 300 year time span. ");
//prints number value
System.out.println(" Numbers are in millions");
//number of rows and columns
final int ROWS = 6;
final int COLUMNS = 7;
//population numbers array
int [] [] populations =
{
{106, 107, 111, 133, 221, 767, 1766},
{502, 635, 809, 947, 1402, 3634, 5268},
{2, 2, 2, 6, 13, 30, 46},
{163, 203, 276, 408, 547, 729, 628},
{2, 7, 26, 82, 172, 307, 392},
{16, 24, 3, 74, 167, 511, 809}
};
//continents array
String[] continents =
{
"Africa",
"Asia",
"Australia",
"Europe",
"North America",
"South America"
};
//prints the years used in the estimation
System.out.println(" Year 1750 1800 1850 1900 1950 2000 2050");
//print population data
for (int i = 0; i < ROWS; i++)
{
//print i'th row
System.out.printf("%20s", continents[i]);
//prints the columns and the rows
for (int j = 0; j < COLUMNS; j++) {
System.out.printf("%5d", populations [i] [j]);
}
System.out.println(); //start new line at end of row
}
//print column totals
System.out.printf("%20s","World total");
//prints the total estimated population in the years used
for (int j = 0; j < COLUMNS; j++)
{
int total = 0;
for (int i = 0; i < ROWS; i++) {
total = total + populations[i][j];
}
//format of the printed totals
System.out.printf("%5d", total);
}
//starts new line after each row
System.out.println();
}
//end of class and program.
}
You could do something like this:
System.out.format("%15s%s", "", "Test");
There you have a string of length 15 followed by a string of undefined. Because the first string being formatted is an empty string, you will have 15 spaces printed instead.
how do I remove all the spaces without changing the format and what is the best option to do so and if so would I be using the System.out.printf(); or some other directive. please respond with java and not C++. I would really appreciate anyone who helps and will gladly thank them for helping me with my own problems.
public class WorldPopulation{
//main string
public static void main(String[] args) {
//prints title
System.out.println(" This is a estimate of world population growth within 300 year time span. ");
//prints number value
System.out.println(" Numbers are in millions");
//number of rows and columns
final int ROWS = 6;
final int COLUMNS = 7;
//population numbers array
int [] [] populations =
{
{106, 107, 111, 133, 221, 767, 1766},
{502, 635, 809, 947, 1402, 3634, 5268},
{2, 2, 2, 6, 13, 30, 46},
{163, 203, 276, 408, 547, 729, 628},
{2, 7, 26, 82, 172, 307, 392},
{16, 24, 3, 74, 167, 511, 809}
};
//continents array
String[] continents =
{
"Africa",
"Asia",
"Australia",
"Europe",
"North America",
"South America"
};
//prints the years used in the estimation
System.out.println(" Year 1750 1800 1850 1900 1950 2000 2050");
//print population data
for (int i = 0; i < ROWS; i++)
{
//print i'th row
System.out.printf("%20s", continents[i]);
//prints the columns and the rows
for (int j = 0; j < COLUMNS; j++) {
System.out.printf("%5d", populations [i] [j]);
}
System.out.println(); //start new line at end of row
}
//print column totals
System.out.printf("%20s","World total");
//prints the total estimated population in the years used
for (int j = 0; j < COLUMNS; j++)
{
int total = 0;
for (int i = 0; i < ROWS; i++) {
total = total + populations[i][j];
}
//format of the printed totals
System.out.printf("%5d", total);
}
//starts new line after each row
System.out.println();
}
//end of class and program.
}
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.