Skip to content

This is a tutorial project. JavaBasics_Task_169_V0.1 Handling array index out of bounds exception in an RPG inventory system. 250226_0957

License

Notifications You must be signed in to change notification settings

YuriiJavaDev/JavaBasics_Task_169_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

RPG Inventory System (JavaBasics_Task_169_V0.1)

📖 Description

This project demonstrates the ArrayIndexOutOfBoundsException in Java. It simulates an RPG inventory where a player attempts to access an item slot that does not exist. This helps in understanding how to handle errors when working with fixed-size data structures like arrays.

📋 Requirements Compliance

  • Task: Create an inventory array with 3 slots and attempt to access the 10th index.
  • Variable: int[] heroInventory with IDs 101, 102, 103.
  • Mechanism: try-catch block for ArrayIndexOutOfBoundsException.
  • Goal: Catch the error and display a user-friendly warning message.

🚀 Architectural Stack

  • Java 8+

🏗️ Implementation Details

In Java, arrays have a fixed length. Attempting to access an index that is less than 0 or greater than or equal to the array's length triggers an ArrayIndexOutOfBoundsException. By using exception handling, we can prevent the game from crashing and notify the user about the invalid input.

📋 Expected result

Error! This slot does not exist in the backpack. Index out of bounds.

💻 Code Example

Project Structure:

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

Code

package com.yurii.pavlenko;

public class Solution {
    public static void main(String[] args) {

        int[] heroInventory = {101, 102, 103};

        try {
            int item = heroInventory[10];
            System.out.println("Item found: " + item);

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error! This slot does not exist in the backpack. Index out of bounds.");
        }
    }
}

⚖️ 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_169_V0.1 Handling array index out of bounds exception in an RPG inventory system. 250226_0957

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages