RhysD
RhysD

Reputation: 1647

Java print character by character

So I have used a little function called typePhrase, and it allows me to give it any string, and it will print it in the console, letter by letter.

public static String typePhrase(String phrase) {
    for(int i = 0; i < phrase.length(); i++) {
        long start = System.currentTimeMillis();
        while (System.currentTimeMillis() - start < 50) {

        }
        System.out.print(phrase.charAt(i));
    }
    return " ";
}

I am wondering if there is a way to make a function like this, but print lots of letters at once, for instance, every 50 milliseconds it would print out 7 letters all together. The code that I am using now, prints one letter every 50 milliseconds.

Upvotes: 0

Views: 90

Answers (1)

Andy Turner
Andy Turner

Reputation: 140318

Change

for(int i = 0; i < phrase.length(); i++) {

to

for(int i = 0; i < phrase.length(); i += 7) {

and

phrase.charAt(i)

to

phrase.substring(i, Math.min(i + 7, phrase.length())

Upvotes: 2

Related Questions