java.util.scanner

Is it possible to use try with resources along with an input stream?

て烟熏妆下的殇ゞ 提交于 2021-02-08 11:47:19
问题 When implementing try with resources I am creating a Scanner object via Scanner sc = new Scanner(System.in) within () of the try statement. In the try block, I am prompting the user to enter a numeric value, reading it via sc.nextLine() and utilizing parseDouble to convert it to a method. I utilize a do-while loop to re-prompt the user to enter a value if an invalid value was entered initially. However, if the user enters an invalid value, the input stream closes, NumberFormatException is

Scanner delimiter not working as expectedwith input file (Java)

南楼画角 提交于 2021-02-07 23:43:09
问题 I am writing a program to read input from a text file that will always follow the format of char:int, such as the following: A:3 B:1 C:2 D:2 (eof here) I want to read in the characters and their corresponding numbers, ignoring the colons. In my program I have the following declarations and initializations: String fileName = "input.txt"; File file = new File(fileName); Scanner in = new Scanner(file).useDelimiter(":"); Then, I try to read from my file using the following loop: while(in

How to close scanner class if we did not create reference variable for object so how to avoid resource leak warning message

心不动则不痛 提交于 2021-02-07 20:13:46
问题 How to close scanner class if we did not create object for it to avoid warning messages stating unassigned closable value, resource leak ? As I need to get input only once from user, I did not create reference variable for object Scanner class . My declaration to get input int num = new Scanner(System.in).nextInt(); 回答1: i did not created object for Scanner class yes, you did: new Scanner(System.in) In case, you are allowed to use try-with-resource: try(Scanner sc=new Scanner(System.in)){ int

How to close scanner class if we did not create reference variable for object so how to avoid resource leak warning message

北城以北 提交于 2021-02-07 20:13:36
问题 How to close scanner class if we did not create object for it to avoid warning messages stating unassigned closable value, resource leak ? As I need to get input only once from user, I did not create reference variable for object Scanner class . My declaration to get input int num = new Scanner(System.in).nextInt(); 回答1: i did not created object for Scanner class yes, you did: new Scanner(System.in) In case, you are allowed to use try-with-resource: try(Scanner sc=new Scanner(System.in)){ int

Is it necessary to close Scanner when there is no stream underlying?

烂漫一生 提交于 2021-02-07 12:24:16
问题 Directly from this Scanner API: String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*"); System.out.println(s.nextInt()); System.out.println(s.nextInt()); System.out.println(s.next()); System.out.println(s.next()); s.close(); 回答1: First we make our habits; then our habits make us. I'd say close the stream, because ingraining a habit like closing resources in finally blocks make sense. 回答2: I don't think so, but it's best to close it

Should a Scanner only be instantiated only once? if that's the case why so?

时光毁灭记忆、已成空白 提交于 2021-02-07 08:29:29
问题 I know I'm going out on a limb here, but I just can't seem to understand why can't we just create an instance of the Scanner class twice. I'll add an example just in case. import java.util.Scanner; public class Nope { public static void main(String[] args) { System.out.println("What's your name?"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("Welcome " + name + "!"); scanner.close(); // Now System.out.println("where you do live?"); Scanner sc

Should a Scanner only be instantiated only once? if that's the case why so?

孤者浪人 提交于 2021-02-07 08:29:20
问题 I know I'm going out on a limb here, but I just can't seem to understand why can't we just create an instance of the Scanner class twice. I'll add an example just in case. import java.util.Scanner; public class Nope { public static void main(String[] args) { System.out.println("What's your name?"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("Welcome " + name + "!"); scanner.close(); // Now System.out.println("where you do live?"); Scanner sc

NoSuchElementException when using tutorialspoint.com

安稳与你 提交于 2021-02-05 11:43:26
问题 So I was working on my code using this compiler site: https://www.tutorialspoint.com/compile_java_online.php And when I ran my code it have me this error: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at WorkingWithLoops.main(WorkingWithLoops.java:22) This is my code: import java.util.Scanner;

NoSuchElementException when using tutorialspoint.com

我是研究僧i 提交于 2021-02-05 11:43:01
问题 So I was working on my code using this compiler site: https://www.tutorialspoint.com/compile_java_online.php And when I ran my code it have me this error: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at WorkingWithLoops.main(WorkingWithLoops.java:22) This is my code: import java.util.Scanner;

Using scanner class with a GUI

南笙酒味 提交于 2021-02-05 09:28:18
问题 I'm using java swing to create my GUI and using the scanner class to get the information inputted from the JTextFields across to the server. is this possible and if so how? 回答1: No. There is no console, so don't use Scanner. Instead get text when you need it by using TextField's getText() method. 回答2: That isn't how Swing works. Scanner is only for command-line input. If you have a JTextField , just call the .getText() method on it. JTextField myField = new JTextField(); ... String