
Hi,
Is there any way I can have a line of code iterate a number of times set by the user without having a while loop or any other control statements?
Thanks!

import java.util.*;
class RepeatString
{
public static String repeat(String str, int times){
return new String(new char[times]).replace("\0", str);
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter no. of times: ");
int no=input.nextInt();
String st="Hello World";
System.out.println(repeat(st+"\n", no));
}
}

Thanks so much!
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.