java.util.scanner

How to stop waiting for user input?

懵懂的女人 提交于 2019-12-05 10:08:29
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 for input, how to stop it ? I've tried putting it in a Thread and interrupt() it or using boolean as

Exception Handling with Scanner.nextInt() vs. Scanner.nextLine()

允我心安 提交于 2019-12-05 06:29:39
问题 This question is solely for educational purposes. I took the following code from a textbook on Java and am curious why input.nextLine() is used in the catch block. I tried to write the program using input.nextInt() in its place. The program would no longer catch the exception properly. When I passed a value other than an integer, the console displayed the familiar "Exception in thread..." error message. When I removed that line of code altogether, the console would endlessly run the catch

Problem using the nextLine() and hasNextLine() methods of Scanner

走远了吗. 提交于 2019-12-05 05:25:20
I have a log file containing the following data: Shortest path(2)::RV3280-RV0973C-RV2888C Shortest path(1)::RV3280-RV2502C Shortest path(2)::RV3280-RV2501C-RV1263 Shortest path(2)::RV2363-Rv3285-RV3280 From each line, i require the number within the brackets, name of the first protein (RV3280 in the first line) and the name of the last protein (RV2888C in the first line). I have written a code for this using the Scanner object. try{ Scanner s = new Scanner(new File(args[0])); while (s.hasNextLine()) { s.findInLine("Shortest path\\((\\d+)\\)::(\\w+).*-(\\w+)"); // at each line, look for this

How can I get this switch statement to work using a scanner?

回眸只為那壹抹淺笑 提交于 2019-12-04 23:10:38
问题 I'm trying to write a program that will switch any letter of the alphabet (upper or lower cases) into the Phontic alphabet. For example, If I enter "A" or "a" my program will give me (change it to) "Alpha". I've done so much research on this and switch statements but I keep getting stuck. I've realized that I can't use 'char' in a scanner. However, when I change 'char' into a 'String' my switch statement messes up (specifically the toUpperCase in my code gets underlined. I can't see my

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

余生长醉 提交于 2019-12-04 21:59:26
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!! SureshBonam Check the following code...It is for reading integer array public class TakingInput { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("enter number of elements"); int n=s.nextInt(); int arr[]

Validating input with while loop and scanner

强颜欢笑 提交于 2019-12-04 21:59:21
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? Where do you change choice inside of your while loop? If it's not changed you can't expect to use it in your if block's boolean condition. You'll have to check that the Scanner doesn't have an

Basic Syntax for passing a Scanner object as a Parameter in a Function

你离开我真会死。 提交于 2019-12-04 16:52:00
Here is what I wrote which is pretty basic : import java.util.Scanner; public class Projet { /** * @param args * @param Scanner */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Enter a digit"); Scanner in = new Scanner(System.in); getChoice(Scanner); in.close(); } public static int getChoice(Scanner n){ n = in.nextInt(); return n; } } What seems to be wrong here ? I had it working earlier, I had to pass the Scanner type and argument name as a parameter to the function... and simply call that function in the main using Scanner type and argument

Java read txt file to hashmap, split by “:”

做~自己de王妃 提交于 2019-12-04 16:46:10
I have a txt file with the form: Key:value Key:value Key:value ... I want to put all the keys with their value in a hashMap that I've created. How do I get a FileReader(file) or Scanner(file) to know when to split up the keys and values at the colon (:) ? :-) I've tried: Scanner scanner = new scanner(file).useDelimiter(":"); HashMap<String, String> map = new Hashmap<>(); while(scanner.hasNext()){ map.put(scanner.next(), scanner.next()); } trooper Read your file line-by-line using a BufferedReader , and for each line perform a split on the first occurrence of : within the line (and if there is

Keep reading numbers until newline reached using a Scanner

吃可爱长大的小学妹 提交于 2019-12-04 12:31:53
问题 I want to read a couple of numbers from the console. The way I wanna do this is by having the user enter a sequence of numbers separated by a space. Code to do the following: Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int i = sc.nextInt(); //... do stuff with i ... } The problem is, how do I stop when reaching a new line (while keeping the easy-to-read code above)? Adding a !hasNextLine() to the argument above makes it quit the loop instantly. One solution would be reading

Problems with Scanner - Java

那年仲夏 提交于 2019-12-04 11:56:43
I'm trying to have the option of reading a string with multiple words, ie. Los Angeles or New York City. Using scanner.next() for "Departure" and "Arrival" would only read the first one if there were two words and split them between variables. nextLine() has not been much luck either. Here's my code: System.out.print("\nEnter flight number: "); int flightNumber = Integer.valueOf(scanner.nextLine()); System.out.print("\nEnter departing city: "); String departingCity = scanner.nextLine(); System.out.print("\nEnter arrival city: "); String arrivalCity = scanner.nextLine(); I know it's something