Question
JAVA - please read ALL the instructions, thank you! Write an application class, named LineScanner , that opens the file numbers.text and prints out the
JAVA - please read ALL the instructions, thank you!
Write an application class, named LineScanner, that opens the file numbers.text and prints out the number of integers on each line of the file.
Again, to accomplish this, read each line into a String using nextLine and then create a second Scanner object using that string as the contructor's argument.
The name of your class should be LineScanner.
The basic code structure of this technique is:
Scanner fileScanner = new Scanner(new File(filename); while (fileScanner.hasNextLine() { // while there are lines left to read String line = fileScanner.nextLine(); // read next line of file Scanner lineScanner = new Scanner(line); // set up a Scanner to read the 'contents of the line' }
This technique can be useful when one want to limit their processing of data to that on a single line of a file.
Using next or nextInt doesn't work because those methods treat newlines like blanks (i.e., all whitespace is treated in the same fashion), so one doesn't know when the end of the line has been reached. While there are several ways of handling this, the above-described techique of reading in a line at a time using nextLine and then creating a new Scanner object from the resulting string returned is a fairly straightforward way of accomplishing this.
IF THE DATA IN THE TEXT FILE LOOKS LIKE BELOW:
1 2 3 5 10 15 20 25 10 9 8 7 6 5 4 3 2 1
the program should produce the following output/RESLTS
There are 3 numbers on line 1 There are 5 numbers on line 2 There are 0 numbers on line 3 There are 10 numbers on line 4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started