This project simulates a common data-entry error where a user provides non-numeric text in a field expected to contain an integer (e.g., age). It focuses on Java's response to invalid format conversion through the Integer.parseInt() method.
- Task: Attempt to parse a non-numeric string into an integer.
- Input:
String playerAgeInput = "abc". - Method:
Integer.parseInt(String). - Goal: Observe the
NumberFormatException.
- Java 8+
The Integer.parseInt() method is designed to scan a string for numeric digits. If it encounters characters that cannot be interpreted as part of a number (like "abc"), it throws a NumberFormatException. In a production environment, this is typically handled within a try-catch block to prevent the application from crashing.
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:588)
at java.base/java.lang.Integer.parseInt(Integer.java:685)
at com.yurii.pavlenko.Solution.main(Solution.java:13)
Project Structure:
src/com/yurii/pavlenko/
└── Solution.java
Code
package com.yurii.pavlenko;
public class Solution {
public static void main(String[] args) {
String playerAgeInput = "abc";
int parsedAge = Integer.parseInt(playerAgeInput);
// This line will never be reached due to the exception
System.out.println("Parsed Player Age: " + parsedAge);
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT