Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have most of the program, the thing I can't figure out is step 11, which is creating the for loop. 3. The type of

image text in transcribed

I have most of the program, the thing I can't figure out is step 11, which is creating the for loop.

3. The type of reader we're using for our file uses a Scanner object, so we also need to import java.util. as well. 4. You'll need a file to read that already exists, so open Notepad. a. b. c. d. e. On the first line, put a String, such as a full sentence: "Java is #1 for business!" On the second line, put a list of 5 integers separated by commas. On the third line, put a list of 5 doubles separated by commas. On the fourth line, put the String "End of file" Save the file in the folder you created for HW1, with a.txt extension. 5. Whenever we do input or output, we need to handle exceptions, so add throws IOException' to the end of the main method header We're going to point the Scanner at the file you created, but Scanner needs you to create a File object out of the file on the hard drive first. Use the File constructor, and give it the absolute path you want to use: 6. File mynputEile-new File("C:/?NFS3310/inputTest.txt"); Now the Scanner has something it can read. Use the Scanner constructor. Earlier we pointed Scanner at System in, but this time we'll use the File object as the argument: 7. Scanner myFileReader new Scanner(myInputFile): 8. Check to see that your reader is working by using the pextLine) method to print each line to the console individually. Since you have four lines, you can just repeat the command four times. (or, you can loop it, which would be good practice). Your output should look something like this Java is #1 for business! 1,1,2,3,5,8,13,21,34,55, 89,144,233 4.5, 3.14, 9.99, 12.6, 7.0 End of file 9. Since you've used the pextline) method four times, the 'cursor' is now at the end of the file. In oxder.te get to the file content 10. This time, we want to copy each line into an Array, so that we can 'keep' the content and manipulate it however we want 11. Now create a FOR loop that will use the pextline0 method of myFileReader2 to collect each line of your file, and store it in again, we will use a new Scanner. Call this one myFileReader2: Scanner myFileReader2 new Scanner(myInputFile) Declare an Array for Strings that holds four elements: String!] my EntireFile new String[4]; a slot in your Array 12. Prove that you now have all your file contents in an Array by printing out ONLY the third line of the Array. Your output from this part should look something like this My Third Line is: 4.5, 3.14, 9.99, 12.6, 7.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Makers And Takers The Rise Of Finance And The Fall Of American Business

Authors: Rana Foroohar

1st Edition

0553447238, 978-0553447231

Students also viewed these Databases questions