/
TextReplacement.java
51 lines (45 loc) · 1.01 KB
/
TextReplacement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.appaholics.scannerprintwriter;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* @author Raghav Sood
* @version 1
*
* Takes text as an input and replaces all occurrences of "key" with
* "replacement"
*
*/
public class TextReplacement {
/**
* java.util.Scanner object
*/
private static Scanner scanner;
/**
* java.io.PrintWriter object
*/
private static PrintWriter printWriter;
/**
* String to store the regex to be replaced
*/
private static String key = "key";
/**
* String to store the replacement of key
*/
private static String replacement = "KEY";
/**
* Main method
*
* @param args
* Not used
*/
public static void main(String[] args) {
scanner = new Scanner(System.in);
printWriter = new PrintWriter(System.out, true);
printWriter.println("Enter a String.");
String text = scanner.nextLine();
scanner.close();
text = text.replaceAll(key, replacement);
printWriter.println(text);
printWriter.close();
}
}