In: Computer Science
This program must be in java, use printf, and request user input for a file name.
Write a program that does the following.
Write a program that does the following.
Requests the name of an input file and uses it to create an input file Scanner.
Requests the name of an output file and uses it to create a PrintWriter.
I have posted firstNames.txt and outNames.txt in the homework for Lesson 6.
Calls createArray to create a one-dimensional String array with 100 rows, and accepts the reference to that array.
Reads in first names from the input file into the array created by createArray. You may assume that the file has no more than 100 names. Also assume that each name has at least 3 characters. (Hint: use the next method). There could be more than one name on a line.
Calls lastThree with a reference to the array created in createArray, and the number of names read in, to create a set of Strings and return a one-dimensional String array as described below.
Prints to the output file on a separate line the String in each row of the array created in createArray followed by a space followed by the String created in lastThree.
createArray: (10)
Creates a one-dimensional String array with 100 rows and returns the reference to that array.
lastThree: (12)
Accepts the reference to the array originally created in createArray and the number of rows that were read in from the file. This array should have a length that equals the number of names read in from the input file.
Obtain the String with last three characters of each name.
Converts each String to upper case, and place each upper case three character String created into a corresponding row in a new one-dimensional String array.
Returns the one-dimensional String array
Example:
If input file is:
Jonathan Jim Bernie
Shondra Nataly
Angel
Brittany Jordan Elvis Ben
The output file, line by line, would be:
Jonathan HAN
Jim JIM
Bernie NIE
Shondra DRA
Nataly ALY
Angel GEL
Brittany ANY
Jordan DAN
Elvis VIS
Ben BEN
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution,
please rate the answer. Thanks
===========================================================================
import java.io.*; import java.util.Scanner; public class NameSubsLast3 { public static void main(String[] args) { //Requests the name of an input file and uses it to create an input file Scanner. Scanner scanner = new Scanner(System.in); System.out.printf("Enter input file name: "); String inputFile = scanner.nextLine(); System.out.printf("Enter output file name: "); String outputFile = scanner.nextLine(); try { //Requests the name of an input file and uses it to create an input // file Scanner. //Requests the name of an output file and uses it to create a PrintWriter. Scanner fileReader = new Scanner(new File(inputFile)); PrintWriter fileWriter = new PrintWriter(new FileWriter(outputFile)); String names[] = createArray(); int nameCount = 0; while (fileReader.hasNext()) { names[nameCount++] = fileReader.next(); } fileReader.close(); String[] lastThree = lastThree(names, nameCount); for (int index = 0; index < nameCount; index++) { fileWriter.write(names[index] + " " + lastThree[index] + "\r\n"); fileWriter.flush(); } fileWriter.close(); } catch (FileNotFoundException e) { System.out.println("Input file is invalid."); System.exit(1); } catch (IOException e) { System.out.println("Output file is invalid."); System.exit(1); } } //Creates a one-dimensional String array with 100 rows and // returns the reference to that array. private static String[] createArray() { return new String[100]; } //Accepts the reference to the array originally // created in createArray and the number of rows // that were read in from the file. This array should // have a length that equals the number of names read // in from the input file. private static String[] lastThree(String[] names, int nameCount) { String[] lastThree = new String[nameCount]; for (int i = 0; i < nameCount; i++) { lastThree[i] = names[i].substring(names[i].length() - 3).toUpperCase(); } //Returns the one-dimensional String array return lastThree; } }