Question

I have a text file that looks like this: Hello a/m The letter "a" is a...

I have a text file that looks like this:

Hello

a/m

The letter "a" is a command that is supposed to append the letter "m" that is followed by the slash to the end of the word "Hello"

How would I write a Java program to read and scan the file, and when the letter "a" is found, it appends "m" to the phrase "Hello"?

The output should print "Hellom"

Homework Answers

Answer #1
import java.io.*;  
import java.util.Scanner; 
import java.util.*;

public class append {
        public static void main(String args[])  
        {  
                try  
                {  
                        //the file to be opened for reading  
                        FileInputStream f=new FileInputStream("G:/Java worksplace/file/src/file/input.TXT");       
                        Scanner sc=new Scanner(f);    //file to be scanned  
                        String s1=""; // initialize the output the string with ""
                        while(sc.hasNextLine())  // Checks if the file has another line to read
                        {  
                                String s= sc.nextLine();
                                if(s.contains("Hello"))
                                        s1=s; // initializes the output string to Hello if it is the first line which contains hello.
                                if(s.startsWith("a"))
                                        s1=s1+s.charAt(2); // when the letter "a" is found, it appends "m" to the phrase "Hello" i.e our output string s1
                        }  
                        System.out.println(s1);
                        sc.close();     //closes the scanner  
                }  
                catch(IOException e)  
                {  
                        e.printStackTrace();  
                }  
        }  
}

This is the screenshot of the program :

This is the screenshot of the Output :

Know the answer?
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
Similar Questions
*java question* I have a program that needs to search if “of my” and “there” are...
*java question* I have a program that needs to search if “of my” and “there” are in a text file but each answer (true or false) has to go into a different output file. if im only allowed to read the text file once is there a way to use scanner to read the text file once and print the answer into two different output files?
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...