Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using java, and the given code below For this assignment, you will complete the given program that is designed for reading names from a file
using java, and the given code below
For this assignment, you will complete the given program that is designed for reading names from a file and storing them in an array. Each line in the file has one name. a Arrays have predetermined sizes. Since we do not know how many names are in the file before we read it, this is problematic. We will find better solutions for this later. In this program, if the number of names in the file is less than the array size, we will read and store all of them in the array. Otherwise, we stop reading the file when the array is full. A print method for arrays is given to you. Notice that the print method stops printing after all "stored strings are printed (or just before the first null value is reached). The heading of getNames method is given to you. You are to complete the method. Method Requirements: 1. The getNames method should work for any size array and work with any file regardless of the number of names in the file. 2. If the number of names in the file is less than or equal to the size of the array, the method should write all the name in the file to the array. 3. If the number of names in the file is greater than the size of the array, the method should fill the array with the names that appear at the top of the array in the same order). 4. The getNames method cannot take any input (such as the number of names in the file) from user. 5. The method can contain only one loop. Simply, loop will read names one at a time from the file and store in the array. The loop should terminate either when the array is full, or end of file is reached. import java.util.Scanner; import java.io.FileReader; import java.io.FileNotFoundException; public class Review1 { //The main method calls a method that throws FileNotFoundException //Therefore we have to declare it. public static void main(String[] args) throws FileNotFoundException { //creating a String array of size 7 String[] names = new String[7]; //call the getNames Method getNames ("list4.txt", names); print(names); } //print method public static void print(String() list) { //if a string is notstored in list[k], it contains null value I/We want to terminate the loop at first nul value. for(int k = 0; kStep 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