0

I'm having trouble with a method I've made. It reads off from a text file, and prints the contents onto the console with a delay, however, while it's printing it can randomly take inputs from the user. Sometimes it intertwines with the contents it's printing. Is there a better way to do this?

public static void filePrint(String location) throws IOException, FileNotFoundException, InterruptedException {
        
        File flScannedFile = new File(location);
        
        Scanner scFileScanner = new Scanner(flScannedFile);
        
        while (scFileScanner.hasNext()) {
    
            String file = scFileScanner.nextLine();
                        
            System.out.print(file);
            
            Thread.sleep(650);
        }
    }
1
  • You seem to be asking a way to prevent user from typing in between the process of printing which I suppose is not possible because Java is platform independent language. Different Operating systems have their own way of accessing console. I don't think there is a way in JAVA to do that. But the helpful thing is that whatever user types in the console, it will shown at last, not in between. And, change your System.out.print(file); to System.out.println(file); It's looks better in printing. Commented Jun 30, 2020 at 4:39

1 Answer 1

0

The user on an IDE will always be able to write on the console, I think this is what is happening to you. The fact that there is a long sleep time is what makes you clearly see that. However, even if the user writes anything on the console, it shouldn't affect the global output.

One thing that I want to add, is that you might want to switch the print to println; and also, it would be better to use a try and catch rather than listing all those exceptions on the method name :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.