Question: Im having trouble with this java problem!! Use List and ArrayList to print a file and average the scores in a file, and use Map

Im having trouble with this java problem!!

Use List and ArrayList to print a file and average the scores in a file, and use Map to create and use a simple English to Spanish dictionary, with possible extra credit to read the dictionary from a file.

Create a PrintFile class that reads a files lines into a List using an ArrayList and then prints them out:

Copy CopyFile.java to PrintFile.java and change the class name

Modify main so that it only expects one command line argument, and if it gets a different number prints out this usage message: Usage: PrintFile filename

Remove all of the logic related to the output file, its not needed in PrintFile

Create a List called list and instantiate it with an ArrayList

In the while loop use add to add the files lines to list like in ArrayListDemo

After closing the input file, print the number of lines in the file, and then use a for-each loop to get them from list and print them out, one per line

Hints: you can either count the number of lines as you are reading them or, after the while loop, the method list.size() returns the number of items in list

Here is the Copyfile.java code:

import java.util.*;

import java.io.*;

public class CopyFile // in CopyFile.java in the Sakai Week 12 Source Code folder

{

public static void main(String[] args) // uses program command line arguments

{

if (args.length != 2) {

System.out.println("Usage: CopyFile inputFile outputFile");

System.exit(0);

}

Scanner inStream = IO.inFile(args[0]);

if (inStream == IO.inError) {

System.out.println("Error: could not read input file " + args[0]);

System.exit(0);

}

PrintStream outStream = IO.newFile(args[1]); // assure output doesnt exist yet

if (outStream == IO.outError) {

System.out.println("Error: could not create output file " + args[1]);

System.exit(0);

} // could also use IO.exists(args[1]) first to check if output file exists

while (inStream.hasNext())

outStream.println(inStream.nextLine());

inStream.close(); // close the input file when done reading from it

outStream.close(); // close the output file when done writing to it

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!