0

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.

Adix
  • 3
  • 2
  • Arrays are indexed `[0...num)`, so `num` is out of bounds. Have a look again range of `i` in `stars` method, notice how it terminates at `i < stars.length`. Why do you return `star[num]` afterwards? – orhtej2 Jun 12 '23 at 10:03
  • What is the point of `return star[num];`? Creating a local array and then only returning a single element makes little sense. That method should either return the whole array you created or return nothing at all and just print directly. I would also recommend reading [Differences between System.out.println() and return in Java](https://stackoverflow.com/questions/25456472/differences-between-system-out-println-and-return-in-java) – OH GOD SPIDERS Jun 12 '23 at 10:07
  • When I was looking for an answer I saw this topic but I didn' found an answer. Now I see my mistakes (Thanks to all who comment). When I back home, I'll correct the code and paste it again. – Adix Jun 12 '23 at 11:34
  • @Adix what do you mean? Please **don't** edit your question to include fixed code. If you feel your question is not a duplicate then please paste revised code alongside some explanation in as an answer. – orhtej2 Jun 12 '23 at 11:53
  • 1
    @orhtej2 sorry for misleading, I meant exactly what you wrote. – Adix Jun 12 '23 at 12:11
  • Without pre-allocation of the array, you can also fill the stars using `String[] stars = Stream.generate(() -> "*").limit(num).toArray(String[]::new);` – WJS Jun 12 '23 at 17:23

2 Answers2

0

Say your array is 3 of length. You will have stars at star[0], star[1], and star[2]. In the stars method, you return star[3] and this doesn't exist.

Pol
  • 482
  • 3
  • 11
0

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
*****
Reilas
  • 3,297
  • 2
  • 4
  • 17