Question

Create a random access Java program to insert 3 missing words into a text file. You...

Create a random access Java program to insert 3 missing words into a text file. You will be supplied with the missing words, which you may hard code in your program. You need to determine from the given text file where the words should be placed!
Missing words are:
details
populate
determine

Randominput.txt
Create a class to hold the x_x_x_x of a new vehicle (include make, model, type, color, number of doors and price as a minimum). Include an ___init___ method to x_x_x_x_ the class object and methods to display the vehicle details and also to calculate the payment based on interest rate, down payment and months of loan. Use a simple interest calculation to x_x_x_x_x the loan details.

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

Code:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.Charset;

public class InsertInFile {
  
   //main method
   public static void main(String[] args) {
       //declaring variables
       String line;
       //this is search sequence for fill in the blanks
       String searchSequence="x_x_x_x";
       //words to be replaced you can change based on question
       String wordToReplace[]={"details","populate","determine"};
       //taking initial finaloutput as empty string
       String finalOutput="";
       //count will be used for total number of words i.e. 3
       int count=0;
       //this will be used so that no space will come in front of line line. e.g. " Count" is wrong it should be "Count" for the first word of line
       int wordCountOfline=1;
       try (
           //creating inputstream and bufferedreader to read from file
       InputStream fis = new FileInputStream("Randominput.txt");
       InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
       BufferedReader br = new BufferedReader(isr);
       ) {
           //loop untill all lines are read..lines will be read one by one
       while ((line = br.readLine()) != null) {
           //split line in words
           String[] words = line.split(" ");
           for (String word : words)
           {
               //foreach word check it will match fill in the blank character
               if(word.contains(searchSequence) && count<3)
               {
                   //if yes then check if word is starting of line
                   if(wordCountOfline==1)
                   {
                       //if yes then add replaced word without space
                   finalOutput=finalOutput+wordToReplace[count];
                   }else{
                       //if no then add replaced word with space
                       finalOutput=finalOutput+" "+wordToReplace[count];
                   }
                   count++;
               }else{
                   //if word does not match fill in the blank charcter then just add to final output
                   if(wordCountOfline==1)
                   {
                   finalOutput=finalOutput+word;
                   }else{
                       finalOutput=finalOutput+" "+word;
                   }
               }
               //increase word count
               wordCountOfline++;
           }
           //add new line characterat the end of line
           finalOutput=finalOutput+System.lineSeparator();
           wordCountOfline=1;
       }
       //opening printwriter to print output
       PrintWriter outputToFile = new PrintWriter("Randominput.txt");
       outputToFile.println(finalOutput);
       //close file
       outputToFile.close();
       //also print output to console
       System.out.println(finalOutput);
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           System.out.println("SomeThingWrong"+e.getMessage());
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           System.out.println("SomeThingWrong"+e.getMessage());
       }

   }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Create a random access Java program to insert 3 missing words into a text file. You...
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
  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • All the white space among words in a text file was lost. Write a C++ program...

    All the white space among words in a text file was lost. Write a C++ program which using dynamic programming to get all of the possible original text files (i.e. with white spaces between words) and rank them in order of likelihood with the best possible runtime. You have a text file of dictionary words and the popularity class of the word (words are listed from popularity 1-100 (being most popular words), 101-200, etc) - Input is a text file...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

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

  • write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File HANDLING..... you have to create functions 1. insert 2. delete 3. search(if we search with any letter then the words...

    write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File HANDLING..... you have to create functions 1. insert 2. delete 3. search(if we search with any letter then the words with same first letter have been shown) 4. display 6. exit Appearance should be like proper dictionary.

  • JAVA: create a program that reads all text files in a directory, calculates the occurrences of...

    JAVA: create a program that reads all text files in a directory, calculates the occurrences of words, and saves the result in one single output text file. assume that you are in a directory called Desktop/programdirectory, where you have multiple text files. textfile1.txt : "how are you" textfile2.txt: "great thank you" textfile3.txt: "great to see you. see you later" Then, your output.txt should have: how: 1 are: 1 you: 4 great: 2 thank: 1 to: 1 see: 2 later: 1

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • Hello. I am using a Java program, which is console-baed. Here is my question. Thank you....

    Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...

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