1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. public class FieldValueComparator {
  5. public static void main(String[] args) {
  6. // Define hard-coded limits for each field
  7. Map<String, Integer> fieldLimits = new HashMap<>();
  8. fieldLimits.put("age", 120); // Example age limit
  9. fieldLimits.put("score", 100); // Example score limit
  10. fieldLimits.put("quantity", 1000); // Example quantity limit
  11. Scanner scanner = new Scanner(System.in);
  12. // Get field values from user input
  13. String ageStr = scanner.nextLine();
  14. String scoreStr = scanner.nextLine();
  15. String quantityStr = scanner.nextLine();
  16. // Parse the values to integers, handling potential errors
  17. int age;
  18. try {
  19. age = Integer.parseInt(ageStr);
  20. } catch (NumberFormatException e) {
  21. System.out.println("Invalid age format. Please enter a number.");
  22. return; // Exit if age is invalid
  23. }
  24. int score;
  25. try {
  26. score = Integer.parseInt(scoreStr);
  27. } catch (NumberFormatException e) {
  28. System.out.println("Invalid score format. Please enter a number.");
  29. return; // Exit if score is invalid
  30. }
  31. int quantity;
  32. try {
  33. quantity = Integer.parseInt(quantityStr);
  34. } catch (NumberFormatException e) {
  35. System.out.println("Invalid quantity format. Please enter a number.");
  36. return; // Exit if quantity is invalid
  37. }
  38. // Compare values against the limits
  39. System.out.println("Age comparison:");
  40. if (age > fieldLimits.get("age")) {
  41. System.out.println("Age exceeds limit.");
  42. } else {
  43. System.out.println("Age is within limit.");
  44. }
  45. System.out.println("\nScore comparison:");
  46. if (score > fieldLimits.get("score")) {
  47. System.out.println("Score exceeds limit.");
  48. } else {
  49. System.out.println("Score is within limit.");
  50. }
  51. System.out.println("\nQuantity comparison:");
  52. if (quantity > fieldLimits.get("quantity")) {
  53. System.out.println("Quantity exceeds limit.");
  54. } else {
  55. System.out.println("Quantity is within limit.");
  56. }
  57. scanner.close();
  58. }
  59. }

Add your comment