java.util.scanner

Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class

…衆ロ難τιáo~ 提交于 2019-11-26 20:51:55
问题 I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from 'Archbishop Street'. How do I fix this? import java.util.*; class MyStudentDetails{ public static void main (String args[]){ Scanner s = new Scanner(System.in); System.out.println("Enter Your Name: "); String name = s.next(); System.out.println("Enter Your Age: "); int age = s.nextInt(); System.out

Is it safe not to close a Java Scanner, provided I close the underlying readable?

ぐ巨炮叔叔 提交于 2019-11-26 20:07:36
问题 If I have a method that takes a reader and I want to operate on the reader with a Scanner like so: Scanner scanner = new Scanner(reader); while(scanner.hasNext()) { //blah blah blah } Is it safe not to close scanner ? Documentation says that it "closes this scanner" and then talks about closing the underlying readable. Suppose I don't want to close the readable and instead want the caller to close reader when ready. Is it safe not to close scanner here? 回答1: It depends what you want to be

Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()

只愿长相守 提交于 2019-11-26 20:04:49
问题 My professor tends to do the following to get a number from the user: Scanner scanner = new Scanner(System.in); Integer.parseInt(scanner.nextLine()); What are the benefits as opposed to simply doing scanner.nextInt() ? java.util.Scanner.java has the following in it: public int nextInt() { return nextInt(defaultRadix); } public int nextInt(int radix) { // Check cached result if ((typeCache != null) && (typeCache instanceof Integer) && this.radix == radix) { int val = ((Integer)typeCache)

Close Scanner without closing System.in

被刻印的时光 ゝ 提交于 2019-11-26 19:11:17
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? You can just ignore close by implementing custom decorator. public class UnClosableDecorator extends InputStream { private final InputStream

Read .txt file into 2D Array

旧城冷巷雨未停 提交于 2019-11-26 18:59:52
There are a few of these topics out there, but this problem has a slight twist that makes it different. I'm focused on only half of a larger problem. I'm sure many of you are aware of the magic square problem. Prompt: Assume a file with lines and numbers on each line like the square shown. Write a program that reads info into a two dimensional array of intS. The program should determine if the matrix is a magic square or not. Working Solution: public static int[][] create2DIntMatrixFromFile(String filename) throws Exception { int[][] matrix = {{1}, {2}}; File inFile = new File(filename);

How to interrupt java.util.Scanner nextLine call

时光怂恿深爱的人放手 提交于 2019-11-26 18:32:06
问题 I am using a multi threaded environment were one Thread is constantly listening for user input by repeatedly calling scanner.nextLine() . To end the application, this runloop is stopped by another thread, but the listening thread won't stop until a last user input was made (due to the blocking nature of nextLine() ). Closing the stream seems not to be an option since I am reading from System.in , which returns an InputStream that is not closable. Is there a way to interrupt the blocking of

How can i handle it with scanner (java)?

老子叫甜甜 提交于 2019-11-26 17:58:38
I have a question about scanner please;I working at a small company; we have a software; it generate a big text file; and we must get some useful information from it ; i want write a simple application with java for saving time; could you please guide me ? for example i want this output ; Output RFID : 25 BLUID : 562 WifiID : 2610 RFID : 33 RFID Count : 2 and for example ;this is my Text file, because each generated file with our software has 14000 lines :) -------------------------- AAAAAAAAAAAA;RFID=25; BBBB;BBBBBBBB;BBBBBBBBBB; CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf; fgfdgdf;terter

hasNext() - when does it block and why?

浪尽此生 提交于 2019-11-26 17:29:13
问题 I'm trying to read commands via a Scanner Object. For checking the Input Syntax I use sc.hasNext() (for the case of missing commands). It did work fine for many cases already, but now I have the case that's described in the JavaAPI as "MAY block and wait for Input". When does the hasNext() method block and how can I control it? The funny Thing is that it work's perfectly fine with 3 cases before the block. Also the JavaAPI describes hasNext() as the proper method for checking wether there is

nextDouble() throws an InputMismatchException when I enter a double

房东的猫 提交于 2019-11-26 17:29:12
问题 import java.util.*; class Averager { public static double unlimited() { int count = 0; double sum = 0; Scanner scan = new Scanner(System.in); while(scan.hasNext()) { double d = scan.nextDouble(); sum += d; count++; } double ave = sum/count; return ave; } public static void main(String[] args) { System.out.println(unlimited()+"\n"); } } There is no error when I use integers but if I use numbers with point in it a error appears. $ javac Averager.java; java Averager 0.5 Exception in thread "main

Scanner on text file hasNext() is infinite

北战南征 提交于 2019-11-26 17:16:09
问题 I'm writing a simple program in Java and it requires reading data from a text file. However, I'm having trouble counting lines. The issue seems generic enough for a simple Google search but I may not even be searching the right things. The textbook I'm learning from suggests that to count the number of lines in a text file, you should do something like this: public static int[] sampleDataArray(String inputFile) throws IOException { File file = new File(inputFile); Scanner inFile = new Scanner