java.util.scanner

Extract Integer Part in String

陌路散爱 提交于 2019-11-26 09:09:42
问题 What is the best way to extract the integer part of a string like Hello123 How do you get the 123 part. You can sort of hack it using Java\'s Scanner, is there a better way? 回答1: Why don't you just use a Regular Expression to match the part of the string that you want? [0-9] That's all you need, plus whatever surrounding chars it requires. Look at http://www.regular-expressions.info/tutorial.html to understand how Regular expressions work. Edit: I'd like to say that Regex may be a little

Java — Closing Scanner and Resource Leak

两盒软妹~` 提交于 2019-11-26 08:29:56
问题 I\'m learning Java and working on some projects for fun. One issue that I have run in to is that when I use a Scanner object Eclipse warns me that: Resource Leak: \'scan\' is never closed. So, I added a scan.close(); at the end of my code and that takes care of the warning. The problem comes in because I have other classes in the same package that also use scanner objects and and Eclipse tells me to close scanner in those classes respectively. However, when I do that it seems like it closes

Close Scanner without closing System.in

核能气质少年 提交于 2019-11-26 07:49:00
问题 I\'m trying to re-factor a large and frequently used part of my application into separate methods to make it easier to maintain. Some of these methods asks the user for input and does input validation, so I\'ve used a Scanner and System.in But when I close my Scanner I also close System.in So my question is, can I only prevent System.in being closed by shielding it with CloseShieldInputStream or should I just start passing a Scanner to the methods? 回答1: You can just ignore close by

How to read a text file directly from Internet using Java?

你说的曾经没有我的故事 提交于 2019-11-26 06:46:10
问题 I am trying to read some words from an online text file. I tried doing something like this File file = new File(\"http://www.puzzlers.org/pub/wordlists/pocket.txt\"); Scanner scan = new Scanner(file); but it didn\'t work, I am getting http://www.puzzlers.org/pub/wordlists/pocket.txt as the output and I just want to get all the words. I know they taught me this back in the day but I don\'t remember exactly how to do it now, any help is greatly appreciated. 回答1: Use an URL instead of File for

Scanner NoSuchElementException

家住魔仙堡 提交于 2019-11-26 05:38:55
问题 I\'m having a problem with my Java assignment. I\'m getting an unexpected exception, specifically: java.util.NoSuchElementException: No line found I am using Scanner(System.in) and the program is continually reading nothing and repeating the \"invalid format\" exception text. If I enter a correctly valued int , the first part passes and then the double part immediately goes into this exception. If I enter an incorrectly valued int , then it starts looping the exception. Here is my code:

Scanner error with nextInt() [duplicate]

梦想的初衷 提交于 2019-11-26 05:38:51
问题 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) Closed 2 years ago . I am trying to use Scanner to get an int from the keyboard, but I getting the following error: Exception in thread \"main\" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner

Java Multiple Scanners

不问归期 提交于 2019-11-26 04:53:54
问题 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

Scanner doesn&#39;t see after space

余生长醉 提交于 2019-11-26 04:53:16
问题 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); } 回答1: Change to String name = scanner.nextLine(); instead of String name = scanner.next(); See more on

How to use multiple Scanner objects on System.in?

流过昼夜 提交于 2019-11-26 04:27:54
问题 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 =

Why does hasNextLine() never end?

a 夏天 提交于 2019-11-26 04:26:15
问题 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()