Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In Java, read from a text file and use an array list to read data that you do not assume the size of before execution.
In Java, read from a text file and use an array list to read data that you do not assume the size of before execution. The text file named names.txt contains the following 4 lines:
"Randy Savage" "The Undertaker" "Steve Austin" "John Cena"
___________________________________________________
import java.io.*; import java.util.*; public class ReadFile { public static ArrayList readLineByLine(BufferedReader reader)throws IOException{ // create input String // create ArrayList // create a while loop that continues to add to the ArrayList from the reader as long as the input is not null // return the ArrayList } public static void writeToFile(String [][] wrestlers, String path)throws IOException{ // create a name of the output file, Ex/ "wrestlers.txt" // I am giving you the code to find the parent directory and the path of the new output file File file = new File(path); String parentDirectory = path.substring(0,file.getAbsolutePath().lastIndexOf(File.separator)); String newPath = parentDirectory + "/" + newFile; System.out.println("Written to: " + newPath); // create a BufferedWriter object to write to the newPath previously created // use a for loop to go through the 2D array and writes the name and the age to the file // close the BufferedWriter object } public static String[] convertToArray(ArrayList names){ // create a string array and allocate the size of the array with the size of the ArrayList // use a for loop to go through the ArrayList and populate the String array with the contents of the array list // return the String array } public static String[][] getAge(String[] actors){ // create a Scanner; // create a 2D String array with [length of actors][2] as dimesions of the array // use a for loop to go through the String array to populate the 2d String array // ask for the actor's age from the array (print out the name at the index x) // read the age as a string // write the name to the first element of the array at index x // write the age to the second element of the array at index x // return the 2D array } public static void main(String [] args)throws IOException{ // replace the path here with the correct path to the txt file given on blackboard String path = "path of/names.txt"; try{ // create FileReader object that reads from path variable // create BufferedReader object that reads from FileReader object // pass the BufferedReader to the readLineByLine method to get an ArrayList back // pass ArrayList to convertToArraty method to get a single String array back // pass the String array to the getAge method to get the 2D String array back // pass the 2d String array to the writeToFile method to write the contents to file }catch(FileNotFoundException ex) { System.out.println( "Unable to open file '" + path + "'"); } } }
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