Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java : Ask a user to enter both a key character , and also a target string (p

ID: 660948 • Letter: I

Question

In Java: Ask a user to enter both a key character, and also a target string (phrase, sentence, etc.). Then, show the user three things::

The target string with the key character replaced by dollar-signs.

The target string with the key character removed.

The number of occurrences of the key character (case sensitive) in the target string.

This program does not loop for different strings. Once it processes a string, it ends.

All methods that are used will be static.

Whenever the user makes an error, comeback and have them try again until they get it right.

Input Method Specs

char getKeyCharacter()

This method requests a single character from the user and continues to ask for it until the user gets it right: this method will test to make sure the user only types one single character. 0, 2, 3 or more characters will be flagged as an error and the method will keep at the user until he types just one character. You are not required to use a char as an input variable -- in fact, you cannot solve the problem using a char as input (you must think about this and make the appropriate choice here). What matters is that a char is returned, as a functional return, to the client, main().

String getString()

This method requests a string from the user and continues to ask for it until the user gets it right: this method will test to make sure the user only types a string that has at least 4 characters. Make this minimum size a constant (final), and use that symbolic constant, not the literal (4) wherever it is needed. The acquired string will be returned as a functional return.

Processing Method Specs

String maskCharacter(String theString, char keyCharacter)

This method will take both a string and a character as parameters and return a new string that has each occurrence of the key character replaced by a dollar-sign, '$'.

String removeCharacter(String theString, char keyCharacter)

This method will take both a string and a character as parameters and return a new string that has each occurrence of the key character removed, but all other characters left intact.

int countKey(String theString, char keyCharacter)

This method will take both a string and a character as parameters, and return the number of key characters that appear in the string (case sensitive).

Explanation / Answer

Program to replace single character:

Progrm to replace character sequences

Number of occurence of key character:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class WordCount {

public static void main(String[] args) {

// TODO Auto-generated method stub

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Please enter any string :");

int count =0;

try {

String input = bf.readLine();

String[] array = input.split(" ");

System.out.print(" Please enter word to be counted :");

String key = bf.readLine();

for(int i=0;i < array.length;i++){

if(array[i].equals(key))

count++;

}

System.out.print(" The given word occured " + count + " times");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote