Issue
I have a problem with a fairly simple question in Java. To repeat a string, you can use str.repeat from Java 11. However I have the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method repeat(int) is undefined for the type String
The code is as follows:
String rep = "-";
rep = rep.repeat(4);
System.out.println(rep);
With a command-line compilation, no problem, but Eclipse is problematic, it uses
java-11-openjdk-amd64
Thanks for your help
Solution
repeat is Java 11 keyword. You are trying to use repeat from a lower version than Java 11.
modify the code with for loop as below.
String rep = "";
for(int i=0;i<4;i++) {
rep += "-";
}
System.out.println(rep);
Answered By - Gayan Chinthaka