Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA- Object Oriented Programming: Add both files to your src directory in the IDE project files. Fulfill the tasks asked in the comments and run

JAVA- Object Oriented Programming: Add both files to your src directory in the IDE project files. Fulfill the tasks asked in the comments and run the code. For this problem you are to read from a text file and use an array list to read data that you do not assume the size of before execution.

Run:

Enter the age of wrestler Brock Lesnar:

354

Enter the age of wrestler Randy Orton:

54

Enter the age of wrestler Goku:

65

Enter the age of wrestler Vegeta:

76

Sample Code:

names.txt:

"Brock Lesnar" "Randy Orton" "Goku" "Vegeta" 

ReadFile.java:

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 = "/home/neil/Documents/object-oriented-programming/solution/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

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

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

Understand the role of internal marketing and communications.

Answered: 1 week ago