import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class FieldValueComparator {
public static void main(String[] args) {
// Define hard-coded limits for each field
Map<String, Integer> fieldLimits = new HashMap<>();
fieldLimits.put("age", 120); // Example age limit
fieldLimits.put("score", 100); // Example score limit
fieldLimits.put("quantity", 1000); // Example quantity limit
Scanner scanner = new Scanner(System.in);
// Get field values from user input
String ageStr = scanner.nextLine();
String scoreStr = scanner.nextLine();
String quantityStr = scanner.nextLine();
// Parse the values to integers, handling potential errors
int age;
try {
age = Integer.parseInt(ageStr);
} catch (NumberFormatException e) {
System.out.println("Invalid age format. Please enter a number.");
return; // Exit if age is invalid
}
int score;
try {
score = Integer.parseInt(scoreStr);
} catch (NumberFormatException e) {
System.out.println("Invalid score format. Please enter a number.");
return; // Exit if score is invalid
}
int quantity;
try {
quantity = Integer.parseInt(quantityStr);
} catch (NumberFormatException e) {
System.out.println("Invalid quantity format. Please enter a number.");
return; // Exit if quantity is invalid
}
// Compare values against the limits
System.out.println("Age comparison:");
if (age > fieldLimits.get("age")) {
System.out.println("Age exceeds limit.");
} else {
System.out.println("Age is within limit.");
}
System.out.println("\nScore comparison:");
if (score > fieldLimits.get("score")) {
System.out.println("Score exceeds limit.");
} else {
System.out.println("Score is within limit.");
}
System.out.println("\nQuantity comparison:");
if (quantity > fieldLimits.get("quantity")) {
System.out.println("Quantity exceeds limit.");
} else {
System.out.println("Quantity is within limit.");
}
scanner.close();
}
}
Add your comment