File RepeatLettersjava shown above contains an incomplete pr

File RepeatLetters.java (shown above) contains an incomplete program, whose goal is to take as input text from the user, and then print out that text so that each letter of the text is repeated multiple times. Complete that program, by defining a repeatLetters function, that satisfies the following specs:

repeatLetters takes two arguments, called text, times.

The function goes through the letters of text in the order in which they appear in text, and prints each such letter as many times as specified by the argumenttimes.

IMPORTANT: you are NOT allowed to modify in any way the main function.

This is an example run of the complete program:

Solution

RepeatLetters.java

import java.util.Scanner;

public class RepeatLetters
{
  
public static int userInteger(String message)
{
Scanner in = new Scanner(System.in);
int result;
while (true)
{
System.out.printf(message);
String s = in.next();
if (s.equals(\"q\"))
{
System.out.printf(\"Exiting...\ \");
System.exit(0);
}
  
try
{
result = Integer.parseInt(s);
}
catch (Exception e)
{
System.out.printf(\"%s is not a valid number, try again.\ \ \", s);
continue;
}
  
if (result <= 0)
{
System.out.printf(\"%s is <= 0, try again.\ \ \", s);
continue;
}
return result;
}
}
  
  
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
  
while (true)
{
System.out.printf(\"Enter some text, or q to quit: \");
String text = in.nextLine();
if (text.equals(\"q\"))
{
System.out.printf(\"Exiting...\ \");
System.exit(0);
}
  
int times = userInteger(\"Enter number of times (must be > 0): \");
  
repeatLetters(text, times);
System.out.printf(\"\ \ \");
}
}
public static void repeatLetters(String text, int times){
   for(int i=0; i<text.length(); i++){
       for(int j=0; j<times; j++){
           System.out.print(text.charAt(i));
       }
   }
}

}

Output:

Enter some text, or q to quit: hello
Enter number of times (must be > 0): 3
hhheeellllllooo

Enter some text, or q to quit: good morning
Enter number of times (must be > 0): 2
ggoooodd mmoorrnniinngg

Enter some text, or q to quit: b
Enter number of times (must be > 0): 7
bbbbbbb

Enter some text, or q to quit: q
Exiting...

File RepeatLetters.java (shown above) contains an incomplete program, whose goal is to take as input text from the user, and then print out that text so that ea
File RepeatLetters.java (shown above) contains an incomplete program, whose goal is to take as input text from the user, and then print out that text so that ea

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site