import java.util.InputMismatchException;
import java.util.Scanner;
public class PopulationDensityComparator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input for Country 1
String country1Name = getInputString(scanner, "Enter the name of the first country: ");
long country1Population = getPositiveLongInput(scanner, "Enter the population of " + country1Name + " (in people): ");
double country1Area = getPositiveDoubleInput(scanner, "Enter the area of " + country1Name + " (in square kilometers): ");
// Input for Country 2
String country2Name = getInputString(scanner, "Enter the name of the second country: ");
// Ensure country2 is different from country1
while (country2Name.equalsIgnoreCase(country1Name)) {
System.out.println("Please enter a different country.");
country2Name = getInputString(scanner, "Enter the name of the second country: ");
}
long country2Population = getPositiveLongInput(scanner, "Enter the population of " + country2Name + " (in people): ");
double country2Area = getPositiveDoubleInput(scanner, "Enter the area of " + country2Name + " (in square kilometers): ");
// Calculate Population Densities
double country1Density = calculatePopulationDensity(country1Population, country1Area);
double country2Density = calculatePopulationDensity(country2Population, country2Area);
// Display Results
displayComparisonResults(country1Name, country1Density, country2Name, country2Density);
scanner.close();
}
/**
* Calculates the population density given the population and area.
*
* @param population The population of the area.
* @param area The area in square kilometers.
* @return The population density (people per square kilometer).
*/
private static double calculatePopulationDensity(long population, double area) {
if (area <= 0) {
throw new IllegalArgumentException("Area must be greater than zero.");
}
return (double) population / area; // Cast to double for precise division
}
/**
* Displays the population density comparison results for two countries.
*
* @param country1Name The name of the first country.
* @param country1Density The population density of the first country.
* @param country2Name The name of the second country.
* @param country2Density The population density of the second country.
*/
private static void displayComparisonResults(String country1Name, double country1Density, String country2Name, double country2Density) {
System.out.printf("%nPopulation Density Comparison:%n");
System.out.printf("- %s: %.2f people/km²%n", country1Name, country1Density);
System.out.printf("- %s: %.2f people/km²%n", country2Name, country2Density);
if (country1Density > country2Density) {
System.out.printf("%n%s has a higher population density than %s.%n", country1Name, country2Name);
} else if (country1Density < country2Density) {
System.out.printf("%n%s has a higher population density than %s.%n", country2Name, country1Name);
} else {
System.out.printf("%n%s and %s have the same population density.%n", country1Name, country2Name);
}
}
// Helper method to get and validate string input
private static String getInputString(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
String input = scanner.nextLine().trim();
if (!input.isEmpty()) {
return input;
}
System.out.println("Input cannot be empty. Please try again.");
}
}
// Helper method to get and validate positive long input
private static long getPositiveLongInput(Scanner scanner, String prompt) {
while (true) {
try {
System.out.print(prompt);
long input = scanner.nextLong();
scanner.nextLine(); // Consume newline left-over
if (input > 0) {
return input;
} else {
System.out.println("Please enter a positive number.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
scanner.nextLine(); // Clear invalid input
}
}
}
// Helper method to get and validate positive double input
private static double getPositiveDoubleInput(Scanner scanner, String prompt) {
while (true) {
try {
System.out.print(prompt);
double input = scanner.nextDouble();
scanner.nextLine(); // Consume newline left-over
if (input > 0) {
return input;
} else {
System.out.println("Please enter a positive number.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
scanner.nextLine(); // Clear invalid input
}
}
}
}