java.util.scanner

How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner

允我心安 提交于 2019-11-25 22:55:55
问题 So, I\'m getting stuck with this piece of code: import java.util.InputMismatchException; import java.util.Scanner; public class ConsoleReader { Scanner reader; public ConsoleReader() { reader = new Scanner(System.in); //reader.useDelimiter(System.getProperty(\"line.separator\")); } public int readInt(String msg) { int num = 0; boolean loop = true; while (loop) { try { System.out.println(msg); num = reader.nextInt(); loop = false; } catch (InputMismatchException e) { System.out.println(\

Take a char input from the Scanner

蓝咒 提交于 2019-11-25 22:44:57
问题 I am trying to find a way to take a char input from the keyboard. I tried using: Scanner reader = new Scanner(System.in); char c = reader.nextChar(); This method doesn\'t exist. I tried taking c as a String . Yet, it would not always work in every case, since the other method I am calling from my method requires a char as an input. Therefore I have to find a way to explicitly take a char as an input. Any help? 回答1: You could take the first character from Scanner.next : char c = reader.next()

Close a Scanner linked to System.in

烈酒焚心 提交于 2019-11-25 22:33:58
问题 I have a Scanner linked to System.in . Now, after using the Scanner , I should close it, as it is bad coding practice to leave it open. But, if I close the Scanner , I will also be closing System.in ! Can anyone tell me how I can close the Scanner without closing System.in (if there is any way). 回答1: One option is to wrap your System.in stream in a CloseShieldInputStream that prevents it from being closed. Your reader would then use the CloseShieldInputStream rather than the raw System.in

Using scanner.nextLine() [duplicate]

谁说胖子不能爱 提交于 2019-11-25 22:08:45
问题 This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 16 answers I have been having trouble while attempting to use the nextLine() method from java.util.Scanner. Here is what I tried: import java.util.Scanner; class TestRevised { public void menu() { Scanner scanner = new Scanner(System.in); System.out.print(\"Enter a sentence:\\t\"); String sentence = scanner.nextLine(); System.out.print(\"Enter an index:\\t\"); int index = scanner.nextInt(

Read CSV with Scanner()

可紊 提交于 2019-11-25 21:58:01
My csv is getting read into the System.out, but I've noticed that any text with a space gets moved into the next line (as a return \n) Here's how my csv starts: first,last,email,address 1, address 2 john,smith,blah@blah.com,123 St. Street, Jane,Smith,blech@blech.com,4455 Roger Cir,apt 2 After running my app, any cell with a space (address 1), gets thrown onto the next line. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class main { public static void main(String[] args) { // -define .csv file in app String fileNameDefined = "uploadedcsv/employees

What's the difference between next() and nextLine() methods from Scanner class?

孤人 提交于 2019-11-25 21:45:18
问题 What is the main difference between next() and nextLine() ? My main goal is to read the all text using a Scanner which may be \"connected\" to any source (file for example). Which one should I choose and why? 回答1: I always prefer to read input using nextLine() and then parse the string. Using next() will only return what comes before the delimiter (defaults to whitespace). nextLine() automatically moves the scanner down after returning the current line. A useful tool for parsing data from

How can I read input from the console using the Scanner class in Java?

孤街浪徒 提交于 2019-11-25 21:44:01
问题 How could I read input from the console using the Scanner class? Something like this: System.out.println(\"Enter your username: \"); Scanner = input(); // Or something like this, I don\'t know the code Basically, all I want is have the scanner read an input for the username, and assign the input to a String variable. 回答1: A simple example to illustrate how java.util.Scanner works would be reading a single integer from System.in . It's really quite simple. Scanner sc = new Scanner(System.in);

Validating input using java.util.Scanner [duplicate]

瘦欲@ 提交于 2019-11-25 21:39:36
问题 This question already has an answer here: How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer I\'m taking user input from System.in using a java.util.Scanner . I need to validate the input for things like: It must be a non-negative number It must be an alphabetical letter ... etc What\'s the best way to do this? 回答1: Overview of Scanner.hasNextXXX methods java.util.Scanner has many hasNextXXX methods that can be used to validate input. Here's a

Scanner is skipping nextLine() after using next() or nextFoo()?

删除回忆录丶 提交于 2019-11-25 21:35:52
问题 I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println(\"Enter numerical value\"); int option; option = input.nextInt(); // Read numerical value from input System.out.println(\"Enter 1st string\"); String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println(\"Enter 2nd string\"); String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value) The problem is that

How to use java.util.Scanner to correctly read user input from System.in and act on it?

痴心易碎 提交于 2019-11-25 21:35:41
问题 This is meant to be a canonical question/answer that can be used as a duplicate target. These requirements are based on the most common questions posted every day and may be added to as needed. They all require the same basic code structure to get to each of the scenarios and they are generally dependent on one another. Scanner seems like a \"simple\" class to use, and that is where the first mistake is made. It is not simple, it has all kinds of non-obvious side effect and aberrant behaviors