java.util.scanner

Scanner is never closed

社会主义新天地 提交于 2019-11-26 16:14:12
问题 I'm working on a game and I came across a little problem with my scanner. I'm getting a resource leak scanner never closed. But I thought my scanner was working before without closing it. But now it ain't. Anyone can help me out here? import java.util.Scanner; public class Main { public static final boolean CHEAT = true; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amountOfPlayers; do { System.out.print("Select the amount of players (1/2): "); while (

How to use multiple Scanner objects on System.in?

百般思念 提交于 2019-11-26 15:31:26
what's the correct way to use multiple Scanner objects in my program. For example, I use scanner to read a file, then depending on what is found in the file, i use scanner again to prompt for user input. An extract of my code is shown .... Scanner f = new Scanner (System.in); //get the file name String fileName = f.next(); Scanner input = new Scanner( new File( fileName ) ); while ( input.hasNext() ) { String currentLine = input.nextLine(); if ( some pattern found) { Scanner getUserInput = new Scanner (System.in); String userInput = getUserInput.next(); ..... } } .... It doesn't seem to work.

Java Multiple Scanners

前提是你 提交于 2019-11-26 15:28:41
I have a class that creates multiple Integer objects and puts them into a LinkedList as shown below: public class Shares<E> implements Queue<E> { protected LinkedList<E> L; public Shares() { L = new LinkedList<E>(); } public boolean add(E price) { System.out.println("How many of these shares would you like?"); Scanner scanInt; scanInt = new Scanner(System.in); Integer noShares = scanInt.nextInt(); for (int i = 0; i < noShares; i++) { L.addLast(price); } scanInt.close(); return true; } } I have an application that scans for the input "add" from the console and if found, invokes the method add

Scanner doesn&#39;t see after space

纵饮孤独 提交于 2019-11-26 15:28:21
I am writing a program that asks for the person's full name and then takes that input and reverses it (i.e John Doe - Doe, John). I started by trying to just get the input, but it is only getting the first name. Here is my code: public static void processName(Scanner scanner) { System.out.print("Please enter your full name: "); String name = scanner.next(); System.out.print(name); } Prabhakaran Change to String name = scanner.nextLine(); instead of String name = scanner.next(); See more on documentation here - next() and nextLine() Try replacing your code String name = scanner.nextLine();

Why does hasNextLine() never end?

拟墨画扇 提交于 2019-11-26 15:27:53
Sorry if this sounds too simple. I'm very new to Java. Here is some simple code I was using to examine hasNextLine() . When I run it, I can't make it stop. I thought if you didn't write any input and pressed Enter , you would escape the while loop. Can someone explain to me how hasNextLine() works in this situation? import java.util.*; public class StringRaw { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String str = sc.nextLine(); } System.out.print("YOU'VE GOT THROUGH"); } } When reading from System.in, you are reading from the

Weird behaviour with Scanner#nextFloat

荒凉一梦 提交于 2019-11-26 14:22:40
问题 Running the following in Eclipse initially caused Scanner to not recognize carriage returns in the console effectively blocking further input: price = sc.nextFloat(); Adding this line before the code causes Scanner to accept 0,23 (french notation) as a float: Locale.setDefault(Locale.US); This is most probably due to regional settings in Windows XP Pro (French/Belgian). When the code is run again 0,23 is still accepted and entering 0.23 causes it to throw a java.util.InputMismatchException .

How to put a Scanner input into an array… for example a couple of numbers

偶尔善良 提交于 2019-11-26 13:59:34
问题 Scanner scan = new Scanner(System.in); double numbers = scan.nextDouble(); double[] avg =..???? 回答1: You could try something like this: public static void main (String[] args) { Scanner input = new Scanner(System.in); double[] numbers = new double[5]; for (int i = 0; i < numbers.length; i++) { System.out.println("Please enter number"); numbers[i] = input.nextDouble(); } } It seems pretty basic stuff unless I am misunderstanding you 回答2: You can get all the doubles with this code: List<Double>

Scanner method to get a char

两盒软妹~` 提交于 2019-11-26 13:28:52
What is the Scanner method to get a char returned by the keyboard in Java. like nextLine() for String , nextInt() for int , etc. To get a char from a Scanner , you can use the findInLine method. Scanner sc = new Scanner("abc"); char ch = sc.findInLine(".").charAt(0); System.out.println(ch); // prints "a" System.out.println(sc.next()); // prints "bc" If you need a bunch of char from a Scanner , then it may be more convenient to (perhaps temporarily) change the delimiter to the empty string. This will make next() returns a length-1 string every time. Scanner sc = new Scanner("abc"); sc

Getting User input with Scanner

蓝咒 提交于 2019-11-26 12:33:27
问题 I am trying to have a scanner take input in a loop. Once the user wants to finish he can exit this loop. I have tried many different ways to do it but there is always some problem. This is the code: private void inputEntries() { Scanner sc = new Scanner(System.in); System.out.println(\"Continue?[Y/N]\"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase(\"y\"))) {//change here System.out.println(\"Enter first name\"); String name = sc.nextLine(); System.out.println(\"Enter surname\");

How do I keep a Scanner from throwing exceptions when the wrong type is entered?

我与影子孤独终老i 提交于 2019-11-26 12:27:54
Here's some sample code: import java.util.Scanner; class In { public static void main (String[]arg) { Scanner in = new Scanner (System.in) ; System.out.println ("how many are invading?") ; int a = in.nextInt() ; System.out.println (a) ; } } If I run the program and give it an int like 4 , then everything goes fine. On the other hand, if I answer too many it doesn't laugh at my funny joke. Instead I get this(as expected): Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner