Question

JAVA help Need help with this part of the code: If the File exists, read from...

JAVA help Need help with this part of the code: If the File exists, read from the File. Capitalize the first letter in the file and capitalize the 1st letter after every period and correct the spacing by removing any duplicate spaces so that there is only 1 space between words. Write the modified output to a file named HomeworkOutput6-2.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.io.*;

import java.util.*;

public class ReadAndEditFile {

public static void main(String[] args) {

String fileName;

Scanner in = new Scanner(System.in);

System.out.print("What is the name of your file? ");

fileName = in.next();

boolean fileExists = true;

Scanner fileIn = null;

do {

try {

fileIn = new Scanner(new File(fileName));

fileExists = true;

} catch (FileNotFoundException e) {

System.out.println("File Not Found " + fileName);

fileExists = false;

System.out.print("Reenter name of file: ");

fileName = in.next();

}

} while (!fileExists);

try {

PrintWriter pw = new PrintWriter(new File("HomeworkOutput6-2.txt"));

while (fileIn.hasNextLine()) {

String line = fileIn.nextLine();

String words[] = line.split(" ");

String newStr = ((words[0].charAt(0) + "").toUpperCase()) + words[0].substring(1).toLowerCase();

for (int i = 1; i < words.length; i++){

newStr += " " + words[i];

if(words[i].indexOf(".")!=-1 && (i+1)<words.length)

words[i+1] = ((words[i+1].charAt(0) + "").toUpperCase()) + words[i+1].substring(1).toLowerCase();

}

pw.println(newStr);

}

pw.close();

} catch (FileNotFoundException e) {

System.out.println("Unable to open file");

}

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA help Need help with this part of the code: If the File exists, read from...
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
  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • I need help parsing a large text file in order to create a map using Java....

    I need help parsing a large text file in order to create a map using Java. I have a text file named weather_report.txt which is filled with hundreds of different indexes. For example: one line is "POMONA SUNNY 49 29 46 NE3 30.46F". There are a few hundred more indexes like that line with different values in the text file and they are not delimited by commas but instead by spaces. Therefore, in this list of indexes we only care...

  • Need help understanding this question from CodeStepByStep in Java, any help is appreciated! Write a method...

    Need help understanding this question from CodeStepByStep in Java, any help is appreciated! Write a method named coinFlip that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of whitespace-separated tokens. Assume that the input file data represents results of sets of coin flips. A coin flip is either the letter H or T, or the word Heads or Tails, in either upper or lower case, separated by at...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Java code to read from .csv file i currently have a .csv file that looks like...

    Java code to read from .csv file i currently have a .csv file that looks like this: 39.743222, -105.006241, Hospital 39.743981, -105.020017, Home 39.739377, -104.984774, Firehouse 39.627779, -104.839291, McDonald's 39.731919, -104.961814, Chipotle I need to write code to extract the doubles from each line to use for my Linked List. this is what i have so far: Scanner in = new Scanner(new FileInputStream(DATA_FILE));

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • 6. Code writing: There exists an input file named “students.txt” that contains student NetIDs and GPAs....

    6. Code writing: There exists an input file named “students.txt” that contains student NetIDs and GPAs. An example is shown on the right; the format is one student per line, with one or more spaces between the NetID and GPA. Write a complete C program that opens this file, inputs the data, and prints the NetID and GPA of every student with a GPA < 2.0; assume the file will open successfully. User-defined functions are not required, and arrays are...

  • 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...

  • I need help with this Java question. Please only answer in Java. Also, please answer it...

    I need help with this Java question. Please only answer in Java. Also, please answer it as simple as possible thanks. Create an application that reads a file that contains an email list, reformats the data, and writes the cleaned list to another file. Console File Cleaner Source file:  prospects.csv Cleaned file: prospects_clean.csv Congratulations! Your file has been cleaned! The prospect.csv file FIRST,LAST,EMAIL james,butler,jbutler@random.com Josephine,Darakjy,josephine_darakjy@darakjy.org ART,VENERE,ART@VENERE.ORG The prospect_clean.csv file First,Last,email James,Butler,jbutler@random.com Josephine,Darakjy,josephine_darakjy@darakjy.org Art,Venere,art@venere.org Specifications Your instructor should provide a CSV file...

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