Answer to Question #161645 in Java | JSP | JSF for aj

Question #161645

Write a Java program to find a certain character in a string and replace it with a new character.

Allow user to enter a string, find character and the replace character.

Display the new string.



1
Expert's answer
2021-02-06T00:58:58-0500
import java.util.Scanner;

public class Q161645 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String string, findChar, replaceChar, newString;
		
		System.out.print("Enter String:");
		string = scanner.next(); // Get user input for initial string
		
		System.out.print("Enter find character: ");
		findChar = scanner.next(); // Get user input for find character
		
		System.out.print("Enter replace character: ");
		replaceChar = scanner.next(); // Get user input for replace character
		
		/* Replace each find character in initial string with replace character */
		newString = string.replace(findChar, replaceChar); 
		System.out.println("The new string is " + newString);
		
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog