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 prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
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.
You are to create a program to request user input and store the data in an...
You are to create a program to request user input and store the data in an array of structures, and then display the data as requested. The data you are collecting refers to a set of images. The images are OCT images taken of the lining of the bladder. The data you provide will help diagnose the image as cancerous or not, but your code does not need to do that at the moment. First, Define a global structure that...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
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.
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
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.
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 prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT