java.util.scanner

FileNotFoundException even when the file is there

[亡魂溺海] 提交于 2019-11-30 09:47:51
问题 public StormAnalysis(){ try { fScanner = new Scanner(new File("tracks1949to2010_epa.txt")); while(fScanner.hasNextLine()){ System.out.println(fScanner.nextLine()); } } catch (FileNotFoundException e) { System.out.println("File not found. Try placing the tracks1949to2010_epa.txt in the same folder as StormAnalysis.java"); e.printStackTrace(); } } The above is my code (and I also have an image of the error : http://folk.uio.no/arnabkd/test/images/error-code-task.jpg As you can see, the txt file

Scanner only reads first word instead of line

半腔热情 提交于 2019-11-30 08:00:50
问题 In my current program one method asks the user to enter the description of a product as a String input. However, when I later attempt to print out this information, only the first word of the String shows. What could be the cause of this? My method is as follows: void setDescription(Product aProduct) { Scanner input = new Scanner(System.in); System.out.print("Describe the product: "); String productDescription = input.next(); aProduct.description = productDescription; } So if the user input

Java Scanner newline recognition

China☆狼群 提交于 2019-11-30 07:40:54
问题 I can't find the documentation that specifies how a Scanner treats newline patterns by default. I want to read a file line by line and have the scanner be able to handle \r, \n or \r\n line endings regardless of the system the program is actually running on. If I declare a scanner like so: Scanner scanner = new Scanner(reader); what is the default behaviour? Will it handle all three kinds as described above or do I have to tell it explicitly to do it? 回答1: Looking at the source code for Sun

How to check the end of line using Scanner?

匆匆过客 提交于 2019-11-30 07:03:54
I have searched similar questions, but none helped. Consider a file : hi how are you? where were you? I want to do few operations after the end of every line. If I use next() it wont tell me when I have reached the end of the first line. Also I have seen hasNextLine() but it only tells me if there exists another line or not. Consider using more than one Scanner, one to get each line, and the other to scan through each line after you've received it. The only caveat I must give is that you must be sure to close the inner Scanner after you're done using it. Actually you will need to close all

Difference between BufferedReader and BufferedInputStream

谁说胖子不能爱 提交于 2019-11-30 06:13:32
What are the differences between BufferedReader , BufferedInputStream and Scanner in java? BufferedReader reads the text and BufferedInputStream reads byte . Is there any difference other than this? Igor I guess, the difference is the same as between reader and inputstream: one is character-based, another is byte-based. For example, reader normally supports encoding... Edit: Check this question: The difference between InputStream and InputStreamReader when reading multi-byte characters Kumaresh Babu BufferedInputStream reads the data in the buffer as bytes by using InputStream . BufferedReader

Java Scanner hasNext() doesn't return false

大城市里の小女人 提交于 2019-11-30 06:04:05
问题 I am trying to read input continuously from the user until the user types quit. I am testing it by just typing 'quit' but console.hasNext() doesn't seem to be returning false. In fact it seems that the program is blocked on line 7(hasNext() line). Any ideas? boolean bool = true; while (bool) { System.out.println("Type 'quit':"); Scanner console = new Scanner(System.in); if (console.next().equals("quit")) { System.out.println("You typed quit"); System.out.println("check: " + console.hasNext())

How to get Java scanner to acknowledge blank input?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 05:18:49
问题 I am having trouble getting my program to respond to empty inputs. For example, say I wanted to prompt the user to enter a value for money with type BigDecimal as well as a currency type. Here is what the program in question looks like. public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the amount of money " + "and specify currency (USD or CNY): "); // initialize variable moneyInput BigDecimal moneyInput; // check if moneyInput is numeric

How to read from standard input line by line?

人走茶凉 提交于 2019-11-29 18:48:02
What's the Scala recipe for reading line by line from the standard input ? Something like the equivalent java code : import java.util.Scanner; public class ScannerTest { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ System.out.println(sc.nextLine()); } } } itemState The most straight-forward looking approach will just use readLine() which is part of Predef . however that is rather ugly as you need to check for eventual null value: object ScannerTest { def main(args: Array[String]) { var ok = true while (ok) { val ln = readLine() ok = ln !=

Scanner No Line Found Exception

亡梦爱人 提交于 2019-11-29 18:07:42
I am getting the following exception. java.util.NoSuchElementException: No line found I got this error while writing a larger program which needed to read from a text file, and so decided to do a test. Scanner scan = new Scanner(new File("restrictions.txt"); String s1 = scan.nextLine(); System.out.println(s1); And I still get the exception. I have a text file in the same folder as the class called restrictions.txt which has text in it. What am I doing wrong? new File("restrictions.txt") will look for the file in the "Start dir" of your app - if you're using Eclipse, it's probably the root of

Scanner for long integer, Exception in thread “main” java.util.InputMismatchException

穿精又带淫゛_ 提交于 2019-11-29 18:07:06
I am at the last step to finalize my program, however whenever I enter my integer (long) I get a input mismatch: Compiler message: "Exception in thread "main" java.util.InputMismatchException: For input string: "4388576018402626" at java.util.Scanner.nextInt(Scanner.java:2097) at java.util.Scanner.nextInt(Scanner.java:2050) at CreditCardValidation.main(CreditCardValidation.java:12)" My code is as follows: import java.util.Scanner ; //import Scanner public class CreditCardValidation { public static void main (String[] args){ Scanner kbd = new Scanner(System.in) ; System.out.println("Please