java.util.scanner

Scanner reading large file

混江龙づ霸主 提交于 2019-11-28 12:17:46
I am playing around with the Scanner class for learning purposes and i use it to read a very large file (60.000 lines aprox) without using the Reader class , and it stops reading after approximately 400 lines. Do i have to use a Bufferedreader inside the Scanner's constructor or the problem is something else? I want to know why this is happening. Thanks. My code is the usual code to output all the lines. File file1 = new File("file1"); Scanner in= new Scanner(file1); while (scan.hasNextLine() ) { String str = scan.nextLine(); System.out.println(str); } Ankit Rustagi This issue is usually more

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

时光怂恿深爱的人放手 提交于 2019-11-28 12:07:40
问题 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

Why is Scanner slower than BufferedReader when reading from input?

北慕城南 提交于 2019-11-28 12:05:39
I understand what is Scanner good for, and also when to use Scanner and when BufferedReader. I read a different, yet in some therm similar question Scanner vs. BufferedReader Why is Scanner so slow when I read from the input? I assume it has to do with that there is a small buffer in Scanner, but here I am lost. The original problem is from, Codechef , but I am not interested in that solution. Here is a code example with a given input: Input: 7 3 1 51 966369 7 9 999996 1 And the code import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main

Reading multiple Scanner inputs

烂漫一生 提交于 2019-11-28 11:38:12
What I am trying to do is have multiple inputs that all have different variables. Each variable will be part of different equations. I am looking for a way to do this and, I think I have an idea. I just want to know if this would be legal and, if maybe there is a better way to do this. import java.util.*; public class Example{ public static void main(String args[]){ Scanner dd = new Scanner(System.in); System.out.println("Enter number."); int a = dd.nextInt(); System.out.println("Enter number."); int b = dd.nextInt(); System.out.println("Enter number."); int c = dd.nextInt(); } } If every

Scanner objects in methods and NoSuchElementException [duplicate]

最后都变了- 提交于 2019-11-28 10:49:31
问题 This question already has an answer here: java.util.NoSuchElementException - Scanner reading user input 2 answers I have really tried to find the answer through the threads but still hope to get some feed back. The code below is bad style I think but I don't know why it shoot me a java.util.NoSuchElementException after enter the number since I make two Scanner objects for two methods and I should be able to start a new input. And if I erase the input.close() in inputAndPrintNumber(), it works

Java - Scanner Class - Skipping over the first line when reading a textfile

雨燕双飞 提交于 2019-11-28 10:39:38
问题 When using a Scanner object to read from a textfile, I want it to skip over the very first line in the file. How would I do achieve this? 回答1: Just use file.nextLine() before your while loop. This will skip the first line, as explained in the JavaDoc. And a note about your naming. The Java language has widely accepted conventions. Class Names always start with an upper case letter, and variable names always start with a lower case letter (except constants, but don't worry about that now).

How to insist that a users input is an int?

若如初见. 提交于 2019-11-28 10:37:44
Basic problem here.. I will start off by asking that you please not respond with any code, as that likely will only confuse me further (programming noob). I am looking for a clear explanation on how to solve this issue that I'm having. I have a scanner that reads input from the user. The user is prompted to enter an int value between 1 to 150 (whole numbers only). I obtain the value as follows: Scanner scan = new Scanner(System.in); int input = scan.nextInt(); And continue on with my program, and everything works fine. Unfortunately, the code isn't exactly bulletproof, since any input that is

Restrict string input from user to alphabetic and numerical values [duplicate]

╄→гoц情女王★ 提交于 2019-11-28 10:33:43
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 Basically, my situation requires me to check to see if the String that is defined by user input from the keyboard is only alphabetical characters in one case and only digits in another case. This is written in Java. my current code: switch (studentMenu) { case 1: // Change all four fields System.out.println("Please enter in a first name: "); String firstNameIntermediate = scan.next(); firstName = firstNameIntermediate.substring(0,1).toUpperCase() +

Java: CSV file read & write

孤人 提交于 2019-11-28 10:26:27
I'm reading 2 csv files: store_inventory & new_acquisitions . I want to be able to compare the store_inventory csv file with new_acquisitions . 1) If the item names match just update the quantity in store_inventory. 2) If new_acquisitions has a new item that does not exist in store_inventory , then add it to the store_inventory . Here is what i have done so far but its not very good. I added comments where i need to add taks 1 & 2 . Any advice or code to do the above tasks would be great! thanks. File new_acq = new File("/src/test/new_acquisitions.csv"); Scanner acq_scan = null; try { acq_scan

Resource Leak Error

筅森魡賤 提交于 2019-11-28 09:59:17
问题 Evening all. I am a complete beginner to programming with Java, and I am learning about "Scanner", but when I type this basic code into Eclipse, I get a message saying "Resource leak:'scanner' is never closed. What am I doing wrong? package inputting; import java.util.Scanner; public class Input { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println(scanner.nextLine()); } } 回答1: After finishing using the scanner , you must close with the close