Dhanushka Gayashan Dhanushka Gayashan 5 5 silver badges 16 16 bronze badges. Why would you create a scanner and set it up to read from System. He's reading one line from the console to get the name of the file that the user wants to read from. He just reuses the same alias for both scanners to simplify the code. Jamal 7 7 gold badges 22 22 silver badges 31 31 bronze badges. You could mention a try catch for handling failure to open the file as well though. Than another for loop can display the numbers in the array.
Vrezh Gulyan Vrezh Gulyan 3 3 silver badges 6 6 bronze badges. You need the specify the exact filename, including the file extension, e. The file needs to be in the same directory as the executable if you want to refer to it without any kind of explicit path.
While we're at it, you need to check for an int before reading an int. It is not safe to check with hasNextLine and then expect an int with nextInt. You should use hasNextInt to check that there actually is an int to grab. How strictly you choose to enforce the one integer per line rule is up to you, of course. Iskar Jarak Iskar Jarak 4, 4 4 gold badges 35 35 silver badges 60 60 bronze badges. Thomas Wouters Thomas Wouters 63 1 1 silver badge 8 8 bronze badges.
Avijit Avijit 11 2 2 bronze badges. Rajesh Guptan Rajesh Guptan 1 1 silver badge 2 2 bronze badges. Kyle Falconer Kyle Falconer 7, 5 5 gold badges 48 48 silver badges 64 64 bronze badges. Paths as well. Amir Maleki Amir Maleki 2 2 silver badges 12 12 bronze badges. Since this doesn't answer the question, it's a better fit for a comment. Sadly new users cannot comment. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 0. See more linked questions. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
The default is large enough for most purposes. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. Constructors defined in this class are as follows:. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
File; import java. Skip to content. Change Language. Related Articles. Table of Contents. Improve Article.
Save Article. Like Article. BufferedReader br. String st;. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Next Split String method in Java with examples. Recommended Articles.
0コメント