I am using this code to create a type writer effect within my code. However while the console is typing the message out slowly, users are able to spam enter and other keys that display on screen. This causes issues with scanner statement that I put afterwards.
So my question is, how am I able to stop users from entering characters while stuff is being written slowly? Alternatively do you guys know of any other methods that will disable users from typing during slow typing effect?
public static void slowPrint(String message, long millisPerChar) {
for (int i = 0; i < message.length(); i++)
{
System.out.print(message.charAt(i));
try
{
Thread.sleep(millisPerChar);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
Thanks
P.S To recreate what I am experiencing: I am using this slow typing effect/method in my program at https://github.com/SakshamInABox/Java/blob/master/Millionaire/src/millionaire/Millionare.java . If you guys try run the file after username creation it types "Are you ready to take the hot seat?" slowly and you can spam keys while it is typing that. The slowPrint() method is being called on line 171.
Edit: I have isolated the function and ran it without any scanner present in code, yet users are able to type while it is typing slowly, that's why I am fairly sure its due to this method, though unsure how to stop it from happening