+ 9
[Java] Scan Infinite Lines Without Knowing How Many Lines There Are?
How do I receive unknown numbers of lines inputted from the user without knowing up front how many lines they will enter in?
5 Antworten
+ 13
while (myReader.hasNextLine())
where myReader is your input object. If the method happens to be absent from your class (for some reason, I can't find the declaration of hasNextLine() for BufferedReader), you can also opt for:
while ((myVar = myReader.nextLine()) != null)
https://www.tutorialspoint.com/java/util/scanner_hasnextline.htm
+ 11
Hi, I hope I got your question right !!!
😅
Step 1
Import the necessary classes by including these lines at the beginning of your program:
import java.io. ;
java.util import;
Step 2
Create a Scanner class instance that takes as input the file whose lines you must count, as in this code example:
File input = new File ("myFile.txt"); Scanner iterate = new Scanner (input);
Replace "myFile.txt" with the name of the input file.
Step 3
Count the number of lines in the file using the integrated Scanner support to analyze the lines in the input file:
int numLines = 0; while (iterate.hasNextLine ()) {String currLine = iterate.nextLine (); numLines ++; }
At the end of the loop, the variable "numLines" will contain the number of lines in the input file.
+ 4
Thank you! Hatsy Rei I implemented it into a program and it worked! Thanks alot! :D
https://code.sololearn.com/c9tJUsrhx1VC/#java
+ 2
Norman Raiti Valenzuela Zavala I was asking a different question but you also described and explained how to read information from a text file, so thanks! 😆
+ 1
how can I change language (german or french)