{"id":3661,"date":"2017-01-21T13:02:42","date_gmt":"2017-01-21T13:02:42","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3661"},"modified":"2017-10-19T03:24:58","modified_gmt":"2017-10-19T03:24:58","slug":"codejava-scanner-read-a-full-line","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/codejava-scanner-read-a-full-line\/","title":{"rendered":"[CODE\/JAVA] Scanner &#8211; read a full line"},"content":{"rendered":"<p>I was solving problems at HackerRank to teach a friend how to code in Java and to process the input data it&#8217;s convenient to use Java&#8217;s built in <code>Scanner<\/code> class.<\/p>\n<p>However, in one of the problems we wanted to read a whole line at once and we noticed that the Scanner&#8217;s API is geared towards reading the next token, and it doesn&#8217;t have a method that reads the entire line. It has one method that skips a line and reads from the last point which is very weird to use, so we created a static method to just read the next entire line, keeping it simple.<\/p>\n<pre><code class=\"java\">public static String readLine(Scanner scanner) {\n            Pattern oldDelimiter = scanner.delimiter();\n            scanner.useDelimiter(\"\\\\r\\\\n|[\\\\n\\\\x0B\\\\x0C\\\\r\\\\u0085\\\\u2028\\\\u2029]\");\n            String r = scanner.next();\n            scanner.useDelimiter(oldDelimiter);\n            return r;\n    }\n<\/code><\/pre>\n<p>The <code>Scanner<\/code> uses as its default token delimiter a <code>whitespace<\/code> pattern, so what we do in this method is to temporarily replace the token delimiter for a <code>newline<\/code> delimiter. Once set to <code>newline<\/code> all we need to do is to read the next token, which will be the entirety of the line, and then we put the previous delimiter set back in place.<\/p>\n<p>In Java 8 there&#8217;s a new Linebreak matcher expression <code>\\R<\/code>, however, if we want our method to be backwards compatible (as we don&#8217;t know on which JRE it could be running) we set the equivalent delimiter manually in our function<\/p>\n<pre><code>\\R  Any Unicode linebreak sequence, is equivalent to \\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]\n<\/code><\/pre>\n<p>If you don&#8217;t like the <code>static<\/code> function approach you can always repurpose this code and extend <code>Scanner<\/code> into a custom <code>MyScanner<\/code> class , and add a method <code>nextLine<\/code> that does the same thing<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was solving problems at HackerRank to teach a friend how to code in Java and to process the input data it&#8217;s convenient to use Java&#8217;s built in Scanner class. However, in one of the problems we wanted to read a whole line at once and we noticed that the Scanner&#8217;s API is geared towards [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[583,1568,1567],"class_list":["post-3661","post","type-post","status-publish","format-standard","hentry","category-code","tag-java","tag-readline","tag-scanner"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-X3","jetpack-related-posts":[{"id":156,"url":"https:\/\/www.gubatron.com\/blog\/portscanner-only-60-lines-of-code\/","url_meta":{"origin":3661,"position":0},"title":"PortScanner &#8211; Only 60 lines of code","author":"gubatron","date":"May 19, 2005","format":false,"excerpt":"Hi, here's a simple port scanner I wrote today in Java. I needed to see something on my server, perhaps you can find it useful. I would say its pretty fast, you can tweak the number of threads and timeouts to adjust to your server\/connection. I did't do much testing\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":3661,"position":1},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1269,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","url_meta":{"origin":3661,"position":2},"title":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated","author":"gubatron","date":"April 20, 2009","format":false,"excerpt":"After I received comments from ispak on Flickr I made a few fixes. ispak pointed out that it was a bad idea reading one byte at the time, also I had a gay ass try\/catch that didn't catch any exception :p So now I read 16 byte chunks, and I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"HexViewer - r2","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3443,"url":"https:\/\/www.gubatron.com\/blog\/my-intellij-keymap-shortcuts\/","url_meta":{"origin":3661,"position":3},"title":"IntelliJ \/ Adroid Studio \/ CLion \/ GoLand \/ PyCharm \/ PHPStorm keymap shortcuts I can&#8217;t live without","author":"gubatron","date":"December 27, 2015","format":false,"excerpt":"Download settings.jar (Import with `File -> Import Settings`) I got to IntelliJ after years of using Emacs and Eclipse. Here's how my keymap looks now that I work with Jetbrains tools, these are the keyboard shortcuts I can't live without as a programmer. \u00a0 Legend (Mac keyboard) \u2318 Command \u21e7\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":2926,"url":"https:\/\/www.gubatron.com\/blog\/how-to-check-if-a-java-process-is-running-on-any-local-virtual-machine-programatically\/","url_meta":{"origin":3661,"position":4},"title":"How to check if a Java Process is running on any local Virtual Machine programmatically","author":"gubatron","date":"January 30, 2013","format":false,"excerpt":"Quick and dirty way to check if a Java Process is already running. Useful if you need to run cronjobs periodically and you don't know how long they might take, you can add this check at the beginning of your main, and it'll look for all the local virtual machines\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2941,"url":"https:\/\/www.gubatron.com\/blog\/java-how-to-get-all-the-files-inside-a-folder-and-its-subfolders-without-recursion\/","url_meta":{"origin":3661,"position":5},"title":"java: How to get all the files inside a folder and its subfolders without recursion","author":"gubatron","date":"March 19, 2013","format":false,"excerpt":"Most programmers will do this in a recursive fashion, but doing that is putting yourself at risk of hitting a stack overflow error, and it's 20% slower (according to my tests). Here's my first implementation of a method that will return just the files (cause I didn't need the folders,\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3661"}],"version-history":[{"count":2,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3661\/revisions"}],"predecessor-version":[{"id":3665,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3661\/revisions\/3665"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}