Skip to content

This is a tutorial project. JavaBasics_Task_179_V0.1 Implementing a gold amount converter with NumberFormatException propagation for an RPG game system. 250226_1436

License

Notifications You must be signed in to change notification settings

YuriiJavaDev/JavaBasics_Task_179_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Gold Amount Converter (JavaBasics_Task_179_V0.1)

📖 Description

This project demonstrates the propagation and handling of NumberFormatException. It simulates an RPG game where a player's string input is converted into a numeric gold amount. The utility method delegates the error handling to the caller, ensuring that invalid text input does not crash the game.

📋 Requirements Compliance

  • Task: Create convertToGoldAmount(String playerInput) method.
  • Mechanism: Declare throws NumberFormatException in the method signature.
  • Implementation: Use Integer.parseInt() to convert player input.
  • Goal: Catch the exception in the main method and display a user-friendly error message in English.

🚀 Architectural Stack

  • Java 8+

🏗️ Implementation Details

The conversion logic is encapsulated in a dedicated method. If Integer.parseInt() encounters a string that cannot be parsed into an integer (e.g., "invalid_amount"), it throws a NumberFormatException. This exception propagates to the main method, where the try-catch block intercepts it and prints a descriptive error message.

📋 Expected result

Player Error: Invalid value entered. Number conversion error.

💻 Code Example

Project Structure:

src/com/yurii/pavlenko/
                └── Solution.java

Code

package com.yurii.pavlenko;

public class Solution {

    public static void main(String[] args) {
        try {
            int gold = convertToGoldAmount("invalid_amount");
            System.out.println("Gold amount: " + gold);
        } catch (NumberFormatException e) {
            System.out.println("Player Error: Invalid value entered. Number conversion error.");
        }
    }

    public static int convertToGoldAmount(String playerInput) throws NumberFormatException {
        return Integer.parseInt(playerInput);
    }
}

⚖️ License

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

About

This is a tutorial project. JavaBasics_Task_179_V0.1 Implementing a gold amount converter with NumberFormatException propagation for an RPG game system. 250226_1436

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages