for-loop. You see in this method we use 2 parameters.
void printCharacters(int numberOfExclamationMarks, char exclamationMark) {
for (int i = 1; i <= numberOfExclamationMarks; i++) {
out.printf("%c", exclamationMark);
}
}
Explanation about parameters: This means; we have in another method two variables. We want to have these 2 variables in this method (printCharacters), because we want to use them in this method. So we got a number (you see type int) and a character (you see type char).
So we got the number 4. Instead of 4, we want to have four exclamation marks (!!!!).
To use this you could ask your user to give an input how many times he/she wants to have this character or letter. This could you do as follows from the method void start():
void start() {
out.printf("How many times do you want to print !: ");
int numberOfExclamationMarks = in.nextInt();
printCharacters(numberOfExclamationMarks, '!');
in.close();
}
So the user gives in line 3 the number of exclamation marks he/she wants. Now, we send this number in the printCharacter method to the method we made above.
If you paste those 2 methods in one program you get this:
The whole code
package ConvertNumberToCharactersPackage;
import java.io.PrintStream;
import java.util.Scanner;
class ConvertNumberToCharactersClass {
PrintStream out;
Scanner in;
RepeatCharacter2() {
out = new PrintStream(System.out);
in = new Scanner(System.in);
}
void printExclamationMarks(int numberOfExclamationMarks, char exclamationMark) {
for (int i = 1; i <= numberOfExclamationMarks; i++) {
out.printf("%c", exclamationMark);
}
}
void start() {
out.printf("How many times do you want to print !: ");
int numberOfExclamationMarks = in.nextInt();
printCharacters(numberOfExclamationMarks, '!');
in.close();
}
public static void main(String[] argv) {
new ConvertNumberToCharactersClass().start();
}
}