java.util.scanner

How Do I Safely Scan for Integer Input? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-07 05:21:15
问题 This question already has answers here : How to use Scanner to accept only valid int as input (6 answers) How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner (5 answers) Closed last year . Scanner scanner = new Scanner(); int number = 1; do { try { option = scanner.nextInt(); } catch (InputMismatchException exception) { System.out.println("Integers only, please."); } } while (number != 0); Despite the exception handling, this code will enter an infinite

How to stop waiting for user input?

烂漫一生 提交于 2019-12-07 05:02:31
问题 I'm building a programm to ask multiplication and I want to set up a timer to force the person to give its answer in a given time : if the person answers before the end of the timer : go next multiplication if the timer reach its end, stop waiting user input : go next multiplication For the moment, case 1 can be done, but 2 not, I was thinking about a way to return; from the method within like a Thread or something, bu I don't know how So I'm facing a problem, if a Scanner is open, waiting

How can i detect BlankLine that Scanner has receive?

亡梦爱人 提交于 2019-12-07 01:56:27
I want to get data form text files ,and I use Scanner to get data form text file. It's profile save pattern name status friend friend . . (Blank line) Blank Line is separate each profile.(friend will looping till next line is a Blank Line) john happy james james sad john And i code to get file form text like this try{ Scanner fileIn = new Scanner(new FileReader("testread.txt")); while(fileIn.hasNextLine()){ String line = fileIn.nextLine(); String linename = fileIn.nextLine(); String statusline = fileIn.nextLine(); println("name "+linename); println("status "+statusline); while(/*I asked at

How Match a Pattern in Text using Scanner and Pattern Classes?

烈酒焚心 提交于 2019-12-07 01:35:24
问题 i want to find whether a particular pattern exists in my text file or not. i m using following classes for this : java.util.regex.Pattern and java.util.Scanner; my sample text Line is String Line="DBREF 1A1F A 102 190 UNP P08046 EGR1_MOUSE 308 396"; and, i want to match following kind of pattern : A 102 190 where, at A's position a-z or A-Z but single charter. at 102's position any integer and of any length. at 190's position any integer and of any length. and,My code for pattern matching is:

When should we use console class?

三世轮回 提交于 2019-12-06 21:38:17
问题 I was reading about Console class, and in the very first line, it was written New to Java 6 and when we are running Java SE 6 from command line, then we are typically using console class object So, which means we are implicitly using console class through the command line ?? Then, I started looking for more detail about Console class and I found Input from console class in java and Console link. So, concluded some points Console class are only usable outside the IDE using System.console()

taking user input array in java using scanner class [duplicate]

安稳与你 提交于 2019-12-06 17:35:02
问题 This question already has answers here : How to put a Scanner input into an array… for example a couple of numbers (11 answers) Closed 5 years ago . How to take input from user side for array using scanner class? Any other method will also be appreciated. for example, if we need to create an array and then taking input from user side. Thank you!! 回答1: Check the following code...It is for reading integer array public class TakingInput { public static void main(String[] args) { Scanner s=new

Continuous input commands

為{幸葍}努か 提交于 2019-12-06 16:09:22
My program will read user keyboard commands in the form of " command parameter " with a space in between. It keeps carrying out individual commands until the next command is " exit ". Also, if the user messes up, the program should show an error but continue asking for commands (a feature I don't think I have completed).. Is the following code a good way of implementing this? Could it handle the user simply pressing the enter key w/o a command, junk input, etc? If anything, I would love to know if there is a better idiomatic way implementing this. String command = ""; String parameter = "";

Java Iterate array with a fixed length and take values with Scanner class

白昼怎懂夜的黑 提交于 2019-12-06 16:01:15
How can i get the value of a java array using Scanner class with a fixed length of 2, and which will iterate until its value is equal to a given value? For example; for the following inputs, A G N H D F I wrote a for loop to take the values of fixed array road, in which the length is 2 using Scanner class. for(int i = 0; i<block.length; i++){ System.out.println("enter number"); block[i]=input2.next().charAt(0); } I want to iterate this loop while the user input is {'C','C'}. THat means the array loop shpuld stop if the inputs are as follow; A G N H D F C C How can i write the code to take user

Validating input with while loop and scanner

无人久伴 提交于 2019-12-06 14:50:02
问题 What's the best way about getting a valid integer from a user that is in a specified range (0,20) and is an int . If they enter an invalid integer print out error. I am thinking something like: int choice = -1; while(!scanner.hasNextInt() || choice < 0 || choice > 20) { System.out.println("Error"); scanner.next(); //clear the buffer } choice = scanner.nextInt(); Is this correct or is there a better way? 回答1: Where do you change choice inside of your while loop? If it's not changed you can't

How to stop Java Scanner from accepting input

大兔子大兔子 提交于 2019-12-06 11:49:21
So, I am working on a quiz taking program that works from the command line and the quizzes have time limits. What I want to do, is to stop the quiz right when the user's time is up even if they're in the middle of answering a question. I am using Java's Scanner to get the user's input, so I want to essentially tell the Scanner object to terminate even if it's in the middle of accepting input. Now, I know that I can retroactively punish a user for going over time after the fact, but I simply want the quiz to terminate once the time limit has been exceeded. Is there any way to do this with