Question

Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

Capitalization JAVA

In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file.

Your program should prompt the user for the name of the file to be processed and continue prompting until a valid (existing) file name is entered. Once a valid file name is entered, you should open it and process it line-by-line. Allow the user to run the program repeatedly with different files until they signal that they are done by entering the word "exit".

REQUIREMENTS

  1. The user may enter the word "exit" in upper or lowercase to exit the program.
  2. Just as with all your programs, you should define additional helper methods to simplify your program and/or to eliminate redundancy. At a minimum, you should have a method that processes the file line-by-line and a separate method that capitalizes a single word.
  3. You should define a method to capitalize a word. This method should accept a string parameter and return a capitalized string as its return value. Capitalizing a word means to change the first character to uppercase and all others to lower case. Note that capitalizing a word does not affect non-alphabetic data (such as numbers). Also, note that we are capitalizing tokens, which misses capitalizing words that are not white-space delimited (e.g., the capitalization of "ta-nehisi" is "Ta-nehisi", and not "Ta-Nehisi").
  4. The output lines must be left justified. It is okay to have an extra space character at the end of each line. The capitalized words are separated by a single space in the output regardless of the number of white spaces that separated the words in the input file.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Capitalize {

   //to capitalize a word
   static String capitailizeWord(String str) {
       StringBuffer s = new StringBuffer();
       char ch = ' ';
       for (int i = 0; i < str.length(); i++) {
           if (ch == ' ' && str.charAt(i) != ' ')
               s.append(Character.toUpperCase(str.charAt(i)));
           else
               s.append(str.charAt(i));
           ch = str.charAt(i);
       }
       return s.toString().trim();
   }
   public static void main(String args[]) {
       Scanner input = new Scanner(System.in);
       String fileName = "";
       File file ;
      
       //this while loop is to ask weather user wants run the programme again or not
       while(true)
       {
           //this is for correct file purpose
           while(true)
           {
               System.out.print("Enter Name of the file to be processed :");
               fileName = input.nextLine();
               file = new File(fileName);
               if(file.exists()) {
                   //if file is exist proceeding
                   System.out.println("------Captalizing file contents-----");
                   try {
               Scanner sc = new Scanner(file);
               while (sc.hasNextLine()) {
                   //calling
                   System.out.println(capitailizeWord(sc.nextLine().toLowerCase()));
               }
               sc.close();
               }
               catch (FileNotFoundException e) {
               e.printStackTrace();
               }
                   break;
               }
               else {
                   System.out.println("File Not Exist. Please Try again");
               }
           }
           System.out.println("\nType \"exit\" to exit the programme or press enter key to continue :");
           if(input.nextLine().equalsIgnoreCase("exit")) {
               System.out.println("----Thank You------");
               break;
           }
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

  • Java This assignment will give you practice with line based file processing and scanner methods. Modify...

    Java This assignment will give you practice with line based file processing and scanner methods. Modify the Hours program we did in class. You are going to write a program that allows the user to search for a person by ID. Your program is required to exactly reproduce the format and behavior of the log of execution as follows: Enter an ID: 456 Brad worked 36.8 hours (7.36 hours/day) Do you want to search again? y Enter an ID: 293...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT