This project demonstrates how to parse string-based numeric data (including negative values) into integers and perform arithmetic operations on the resulting data.
- Create two
Stringvariablespoint1Strandpoint2Strcontaining negative numbers (e.g., "-10", "-25"). - Convert (parse) both strings into
intvariables. - Calculate the total score by adding the two integers.
- Output the final result to the console.
- Language: Java 23 (Current version).
- IDE: IntelliJ IDEA.
- Concepts: String Parsing,
Integer.parseInt(), Arithmetic Operations with Negative Numbers.
This task applied the clean code explicit conversion principle. Using Integer.parseInt() allows the program to correctly interpret the minus sign as part of the numeric value during the transformation process.
- Readability: Explicitly parsing each score before calculation makes the mathematical logic clear and separated from data retrieval.
- Precision: Unlike string concatenation (
+), converting tointensures that the+operator performs mathematical addition rather than joining characters. - Data Integrity: This approach validates that the incoming game data can be treated as valid integers before any logic is applied.
Total Game Score: -35
package com.yurii.pavlenko;
/**
* Task 19: Calculating total game score from string inputs.
* Demonstrates parsing negative numeric strings and performing addition.
*/
public class Task_19_App {
public static void main(String[] args) {
// 1. Create String variables with negative numeric values
String point1Str = "-10";
String point2Str = "-25";
// 2. Parse strings into integers (Clean Code approach)
int point1 = Integer.parseInt(point1Str);
int point2 = Integer.parseInt(point2Str);
// 3. Perform addition to get the total score
int totalScore = point1 + point2;
// 4. Output the result to the screen
System.out.println("Total Game Score: " + totalScore);
}
}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