Question

In: Computer Science

This program must be in java, use printf, and request user input for a file name....

This program must be in java, use printf, and request user input for a file name.

  1. NameSubsLast3 (30 points)

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

Solutions

Expert Solution

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;

    }
}


Related Solutions

Write a MIPS Assembly language program to request a file name from the user, open the...
Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console. This is what I have so far: .data    fileName:   .space 100    prompt1:   .asciiz "Enter the file name: "    prompt2:    .asciiz ""    prompt3:   .asciiz "\n"    buffer:    .space 4096 .text    main:        #        li $v0, 4        la $a0, prompt1       ...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
write a java program to evaluate the highest number input by user, cannot use math class,...
write a java program to evaluate the highest number input by user, cannot use math class, must use overloading . Must compute four computeHighNum methods. Here is what I have. import java.util.Scanner; public class Overload { public static void main(String[] args){ Scanner input = new Scanner(System.in); int number1 = input.nextInt(); int number2 = input.nextInt(); int number3 = input.nextInt();    //int maximum = maximum(number1, number2 ); System.out.printf("Highest integer is ");       // int number1 = input.nextInt(); // int number2 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT