Search code examples
javastringloopsmethods

Index X out of bounds for length X


I'm learning Java and while learning methods I came across an error. What's more, code does what I want, (it displays stars), but it also throws an error. I don't quite know why.

Here is code and error below:

import java.util.Scanner;

public class TaskMethods {
    
    //Display the stars
    public static void main (String[] args){
        System.out.println("Insert number of stars:");

    //declare numbers of stars
        int num = getInt();

    //Display the stars by 'stars' method
        System.out.println(stars(num));
    }
    //method for input int variables
    public static int getInt(){
        return new Scanner(System.in).nextInt();
    }

    //method for inputing stars to array and also for displaying them.
    public static String stars(int num){
        String[] star = new String[num];
        for (int i = 0; i < star.length; i++){
            star[i] = "*";
            System.out.print(star[i]);
        }
        return star[num];
    }
}

enter image description here

I tried to limit the loop by the array lenght (like always) and It's nothing. There is no line where we declare values ​​outside the range of the array. In every post I've seen the problem was wrong range.

Thats why I'm looking for help here.


Solution

  • You're attempting to access star[num], which would be 1 outside the last indexed value.

    return star[num];
    

    Additionally, you are supplying the return value of the stars method, to a println call, here.

    //Display the stars by 'stars' method
    System.out.println(stars(num));
    

    Which, isn't necessary, since stars uses print to display the value.

    There are two approaches you can take.

    One, simply call stars(num), without the println call.
    This would also allow you to remove the return type of stars—by changing it to void.

    //method for inputing stars to array and also for displaying them.
    public static void stars(int num){
        String[] star = new String[num];
        for (int i = 0; i < star.length; i++){
            star[i] = "*";
            System.out.print(star[i]);
        }
    }
    

    Or, you can populate a String value within stars, and return that.

    public static String stars(int num){
        String[] star = new String[num];
        String string = "";
        for (int i = 0; i < star.length; i++){
            star[i] = "*";
            string += star[i];
        }
        return string;
    }
    

    Subsequently, you can reduce the string and star[i] assignment, within the for-loop.

    string += star[i] = "*";
    

    And, since we're on the topic, you can utilize the Arrays#fill method, to populate an array, and the String#join method to concatenate the values.

    public static String stars(int num){
        String[] star = new String[num];
        Arrays.fill(star, "*");
        return String.join("", star);
    }
    

    Furthermore, the String#repeat method, can produce a sequence of repeating characters.

    public static String stars(int num){
        return "*".repeat(num);
    }
    

    Here is an example input and output.

    Insert number of stars:
    5
    *****