I have a question. I'm struggling with a exercise where I am asked to get from user a string, a character which I want to duplicate in this string, and a number - how many times I want to duplicate. Eg: string input: dog; character: o; number:4. Output: doooog. My question is how can I achieve this result?
Scanner sc = new Scanner(System.in);
System.out.println("enter your string");
String text = sc.nextLine();
System.out.println("enter the character that will be repeated");
char character= sc.next().charAt(0);
System.out.println("enter the number of repetitions");
int repetitions = sc.nextInt();
for (int i = 0; i < text.length(); i++) {
char z = text.charAt(i);
if(z==character) {
// repeat character in string put by user * repetitions
}
}
System.out.println(text);