java.util.scanner

Is the scanner in java not thread safe?

◇◆丶佛笑我妖孽 提交于 2019-11-29 14:53:27
I'm interested in using java.util.Scanner . I was reading the docs and saw a line saying A Scanner is not safe for multi threaded use without external synchronization. Can I confirm that this means that two separate Scanner objects in two separate threads operating on two separate files could interfere with each other? Can anyone help me to synchronise scanner object externally to use for safe thread operation? Terrible Tadpole If you use the same instance of Scanner in two threads you will have trouble unless you synchronise access to the object. But two separate instances of Scanner will

Scanner Exception Retry

佐手、 提交于 2019-11-29 14:08:53
How to make scanner retry when exception occur? Consider this app running on CLI mode. Example: System.out.print("Define width: "); try { width = scanner.nextDouble(); } catch (Exception e) { System.err.println("That's not a number!"); //width = scanner.nextDouble(); // Wrong code, this bring error. } If the user not inputting double type input, then the error thrown. But i want after the error message appears. It's should be asking the user input the width again. How to do that? Pablo Francisco Pérez Hidalgo If I understood you correctly, you want the program to ask the user to re-enter a

What does scanner.close() do?

最后都变了- 提交于 2019-11-29 13:35:29
问题 Say I have the following example code: Scanner scan1 = new Scanner(System.in); // declaring new Scanner called scan1 int x = scan1.nextInt(); // scan for user input and set it to x System.out.println(x); // print the value of x scan1.close(); // closes the scanner (I don't know exactly what this does) Scanner scan2 = new Scanner(System.in); // declaring new Scanner called scan1 int y = scan2.nextInt(); // scan for user input and set it to y System.out.println(y); // print the value of y I

Java - Parsing Text File

依然范特西╮ 提交于 2019-11-29 11:20:36
I have an input text file in this format: <target1> : <dep1> <dep2> ... <target2> : <dep1> <dep2> ... ... And a method that takes two parameters function(target, dep); I need to get this parsing to call my method with each target and dep eg: function(target1, dep1); function(target1, dep2); function(target1, ...); function(target2, dep1); function(target2, dep2); function(target2, ...); What would be the most efficient way to call function(target,dep) on each line of a text file? I tried fooling around with the scanner and string.split but was unsuccessful. I'm stumped. Thanks. Read line into

How to make Scanner Properly Read Escape Characters?

北战南征 提交于 2019-11-29 10:34:36
I'm reading from a file that reads something like all on one line: Hello World!\nI've been trying to get this to work for a while now.\nFrustrating.\n And my Scanner reads that from the file and puts it in a String: Scanner input = new Scanner(new File(fileName)); String str = input.nextLine(); System.out.print(str); Now, I want the output then to be: Hello World! I've been trying to get this work for a while now. Frustrating. But instead I'm getting the exact same thing as the input. That is, each \n is included in the output and everything is on one line instead of separate lines. I thought

Search a file for a String and return that String if found

半城伤御伤魂 提交于 2019-11-29 07:11:50
How can you search through a txt file for a String that the user inputs and then return that String to the console. I've written some code that doesn't work below, but I hope it can illustrate my point... public static void main(String[] args) { searchforName(); } private static void searchForName() throws FileNotFoundException { File file = new File("leaders.txt"); Scanner kb = new Scanner(System.in); Scanner input = new Scanner(file); System.out.println("Please enter the name you would like to search for: "); String name = kb.nextLine(); while(input.hasNextLine()) { System.out.println(input

Scanner only reads first word instead of line

大城市里の小女人 提交于 2019-11-29 05:54:57
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 is "Sparkling soda with orange flavor", the System.out.print will only yield "Sparkling". Any help will

Difference between BufferedReader and BufferedInputStream

徘徊边缘 提交于 2019-11-29 05:52:39
问题 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? 回答1: 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 回答2:

Explain this line written in JAVA

删除回忆录丶 提交于 2019-11-28 22:09:55
In HACKERRANK this line of code occurs ery frequently. I think this is to skip whitespaces but what does that "\r\u2028\u2029\u0085" thing mean scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); Scanner.skip skips a input which matches the pattern, here the pattern is :- (\r\n|[\n\r\u2028\u2029\u0085])? ? matches exactly zero or one of the previous character. | Alternative [] Matches single character present in \r matches a carriage return \n newline \u2028 matches the character with index 2018 base 16(8232 base 10 or 20050 base 8) case sensitive \u2029 matches the character with index 2029

How to determine when end of file has been reached?

社会主义新天地 提交于 2019-11-28 21:20:40
I am trying to read text from a text file. I need help figuring out when the end of file has occured. How can I determine this in Java? FileInputStream istream = new FileInputStream("\""+filename+"\""); Scanner input = new Scanner(istream); while(EOF != true) { .... } Thanks! You can check using hasNextLine() : Scanner input = new Scanner(new File("\""+filename+"\"")); while(input.hasNextLine()) { String data = input.nextLine(); } Thomas Langston Line based retrieval may be what you want, but token based can also be useful. You can see in documentation of Scanner public boolean hasNext()