Lab 7b – Populating Array From File

Create a file named Lab7b.java in your lab7 directory.  Write a program in Lab7b.java that satisfies the following specifications.


Inside the class (but after the main method) write a method named printArray that takes an array of integers as an argument.  The method uses a for-loop to print to the screen (on a single line with spaces between them) the elements in the array that is passed to the method.

In the main method, do the following:

    • Print to the screen “Populate Array from File”.
    • Ask the user to enter a file name, read the file name from the keyboard, and store the value in a variable named fileName.
    • Create an instance of the Scanner class that can read from the file whose name is in the variable named fileName.
    • Use the Scanner to read the integer value that is on the first line of the file and store it in a variable named size.
    • Declare an array of integers whose length is equal to the value in the variable named size.
    • Read the values on the second line of the file and store them in the array.
    • Print the contents of the array to the screen using the printArray method.

Compile and test your program using the file name random.txt.  Push your code to GitHub when your code is correct.

© 2024, Eric. All rights reserved.