Java Scanner doesn’t wait for user input [duplicate]

It’s possible that you are calling a method like nextInt() before. Thus a program like this: Scanner scanner = new Scanner(System.in); int pos = scanner.nextInt(); System.out.print(“X: “); String x = scanner.nextLine(); System.out.print(“Y: “); String y = scanner.nextLine(); demonstatres the behavior you’re seeing. The problem is that nextInt() does not consume the ‘\n’, so the next … Read more

How to read a text file directly from Internet using Java?

Use an URL instead of File for any access that is not on your local computer. URL url = new URL(“http://www.puzzlers.org/pub/wordlists/pocket.txt”); Scanner s = new Scanner(url.openStream()); Actually, URL is even more generally useful, also for local access (use a file: URL), jar files, and about everything that one can retrieve somehow. The way above interprets … Read more

Getting User input with Scanner

This is because you are using Scanner#next method. And if you look at the documentation of that method, it returns the next token read. So, when you read user input using next method, it does not read the newline at the end. Which is then read by the nextLine() inside the while loop. And thus, … Read more

Java using scanner enter key pressed

This works using java.util.Scanner and will take multiple “enter” keystrokes: Scanner scanner = new Scanner(System.in); String readString = scanner.nextLine(); while(readString!=null) { System.out.println(readString); if (readString.isEmpty()) { System.out.println(“Read Enter Key.”); } if (scanner.hasNextLine()) { readString = scanner.nextLine(); } else { readString = null; } } To break it down: Scanner scanner = new Scanner(System.in); String readString = … Read more