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];
}
}
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.