This project demonstrates how to convert numeric data stored as a String into a primitive int type to enable mathematical operations.
- Create a
StringvariablefilmYearStrcontaining a year (e.g., "1999"). - Convert (parse) this string into an
intvariable. - Output the resulting integer to the console to verify successful conversion.
- Language: Java 23 (Current version).
- IDE: IntelliJ IDEA.
- Concepts: Type Parsing,
Integer.parseInt(), Primitive vs. Object types.
This task applied the clean code explicit conversion principle. Instead of relying on risky implicit behaviors, we use the Integer.parseInt() method, which is the standard Java way to transform text into an integer.
- Readability: Using
Integer.parseInt()clearly signals the intent to extract a numeric value from a string. - Performance: This is a static method optimized by the JVM for fast string-to-primitive conversion.
- Safety: This method ensures that the input is a valid number; if the string contains letters, it will throw a
NumberFormatException.
Extracted film year: 1999
package com.yurii.pavlenko;
/**
* Task 18: Converting string-based year to an integer.
* Demonstrates explicit parsing for numeric operations.
*/
public class Task_18_App {
public static void main(String[] args) {
// 1. Create a String variable containing the year
String filmYearStr = "1999";
// 2. Parse the string into an integer (Clean Code approach)
int filmYear = Integer.parseInt(filmYearStr);
// 3. Output the result to the screen
System.out.println("Extracted film year: " + filmYear);
}
}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