2

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

4
  • Are you sure you're using java 11 for the project your code is in? Also, what version of eclipse are you using? Older versions don't support newer versions of Java. Even if you've got eclipse pointing to the proper JDK, it might not have 11 available as a compiler compliance level. Commented Jan 13, 2020 at 22:15
  • 1
    try running this in eclipse: System.out.println(System.getProperty("java.vm.version")); Commented Jan 13, 2020 at 22:21
  • 1
    Look at the 'Java Compiler' settings in the project Properties and make sure it is set to Java 11 or above. Look at the 'Java Build Path' in the Properties and make sure you are using a Java 11 or above JRE/JDK. Commented Jan 14, 2020 at 7:55
  • Thanks for your answers, these were the configuration files that eclipse generated, it was not using the right version of Java, despite the message displayed. Commented Jan 15, 2020 at 9:08

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.