I wrote below code and i want to identify the test cases where my code fails.I want to identify the possible test cases where my test case failed. so i will improvise the same, in addition I also want to enhance it, meaning,also suggest me the best practice to improve performance.
At last, In the below code I have one method returnFinalComplement(String reverseStr) which return the complement, for calculating complement I have two Approaches, both are doing the same job, I am currenlty using approach-1, but i want to know about approach-2, which approach can pass all the test cases .
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
class DNAString {
private final static Character[] DATASET = {'A', 'C', 'G', 'T'};
private final static int MIN_RANGE = 0;
private final static int MAX_RANGE = 1000;
private final static String INVALID_CHAR = "Invalid input,not matched
with dataset";
private final static String CHAR_LENGTH_EXCEEDS = "Input does not
allow to be exceeds more than " + MAX_RANGE + " characters";
private static String returnFinalComplement(String reverseStr) {
//APPROACH-1
StringBuilder finalStr = new StringBuilder();
for (char c : reverseStr.toCharArray()) {
finalStr.append(
c == 'G' ? 'C'
: c == 'C' ? 'G'
: c == 'T' ? 'A'
: c == 'A' ? 'T'
: c
);
}
return finalStr.toString();
//APPROACH-2
char[] charlist = list.toCharArray();
for (int i = 0; i < charlist.length; i++) {
switch (charlist[i]) {
case 'A': {
charlist[i] = 'T';
}
break;
case 'T': {
charlist[i] = 'A';
}
break;
case 'G': {
charlist[i] = 'C';
}
break;
case 'C': {
charlist[i] = 'G';
}
break;
}
}
return new String(charlist);
}
public static boolean validateInput(String input) {
List<Character> chars = new ArrayList<>();
for (char c : input.toCharArray()) {
chars.add(c);
}
boolean result = false;
SortedSet<Character> mySet = new TreeSet<>(chars);
for (int i = 0; i <= mySet.size();) {
for (Character c : mySet) {
result = Objects.equals(c, DATASET[i]);
i++;
if (!result) {
break;
}
}
if (result) {
return result;
} else {
break;
}
}
return result;
}
public static String reverseIt(String source) {
int i, len = source.length();
StringBuilder dest = new StringBuilder(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
public static void main(String[] args) throws IOException {
BufferedReader readInput = new BufferedReader(new
InputStreamReader(System.in));
String source = readInput.readLine();
if (source.length() > MIN_RANGE && source.length() <= MAX_RANGE) {
boolean validateInput = validateInput(source);
if (validateInput) {
// String reverseString = reverseIt(source);
String reverseString = reverseIt(source);
System.out.println(returnFinalComplement(reverseString));
// String revereStringComplement =
returnFinalComplement(reverseString);
} else {
System.out.println(INVALID_CHAR);
}
} else {
System.out.println(CHAR_LENGTH_EXCEEDS);
}
}
}
Related
I'm trying to detect first level "if" conditions in a piece of text.
Example text:
if (a == 5) {
method1();
method2()
}
if (a == 6) {
method1();
if (a < 2) {
method3();
}
}
if (a >= 8 && a <= 13) {
method5(a);
int[] b = new int[a];
for(int i = 0; i < a; i++) {
if (i == 0) {
b[i] = i * 4;
continue;
}
b[i] = i * 2;
}
method4(b);
}
if (a > 16) {
method6();
}
This is what I got so far:
public class HelloWorld
{
public static void main(String[] args)
{
String text = "if (a == 5) {\n\tmethod1();\n\tmethod2()\n}\nif (a == 6) {\n\tmethod1();\n\tif (a < 2) {\n\t\tmethod3();\n\t}\n}\nif (a >= 8 && a <= 13) {\n\tmethod5(a);\n\tint[] b = new int[a];\n\tfor(int i = 0; i < a; i++) {\n\t\tif (i == 0) {\n\t\t\tb[i] = i * 4;\n\t\t\tcontinue;\n\t\t}\n\t\tb[i] = i * 2;\n\t}\n\tmethod4(b);\n}\nif (a > 16) {\n\tmethod6();\n}";
for(String line : text.split("if (.*) \\{")) {
System.out.println("Line: " + line);
}
}
}
Output:
Line:
Line:
method1();
method2()
}
Line:
method1();
Line:
method3();
}
}
Line:
method5(a);
int[] b = new int[a];
for(int i = 0; i < a; i++) {
Line:
b[i] = i * 4;
continue;
}
b[i] = i * 2;
}
method4(b);
}
Line:
method6();
}
It also prints nested ifs. I only want the first level ones. And the if will disappear when printing the line. I want the if to show too.
I basically want to group all first level ifs into one string. Can some one help me with this?
Since that you have to deal with nested brackets, the appropriate regex will be hard to maintain as described here in SO How to match string within parentheses (nested) in Java?
My solution is:
Do some preprocessing to replace the nested brackets
Capture the if content using regex
Finally, postprocessing to hand over the real brackets
package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Parser {
private static final char OPENED_BRACKET = '{';
private static final char CLOSED_BRACKET = '}';
private static final String OPENED_BRACKET_REPLACOR = "##OPENED_BRACKET_REPLACOR##";
private static final String CLOSED_BRACKET_REPLACOR = "##CLOSED_BRACKET_REPLACOR##";
private static final String REGEX = "\\{((.|\\n|\r|\t)*?)\\}";
private static final Pattern PATTERN = Pattern.compile(REGEX);
public String preprocessing(String origin) {
StringBuilder replaced = new StringBuilder();
int opened = 0;
for(int index = 0 ; index < origin.length() ; index++) {
char current_char = origin.charAt(index);
String processed = Character.toString(current_char);
if(current_char == OPENED_BRACKET) {
if(opened++ > 0) {
processed = OPENED_BRACKET_REPLACOR;
}
}
else if(current_char == CLOSED_BRACKET) {
if(--opened > 0) {
processed = CLOSED_BRACKET_REPLACOR;
}
}
replaced.append(processed);
}
return replaced.toString();
}
public List<String> extract(String source) {
final Matcher matcher = PATTERN.matcher(source);
List<String> list = new ArrayList<>();
while(matcher.find()) {
list.add(matcher.group(1));
}
return list;
}
public List<String> postprocessing(List<String> source) {
List<String> result = new ArrayList<>();
for(String src: source) {
result.add(src.replaceAll(OPENED_BRACKET_REPLACOR, Character.toString(OPENED_BRACKET))
.replaceAll(CLOSED_BRACKET_REPLACOR, Character.toString(CLOSED_BRACKET)));
}
return result;
}
public static void main(String[] args) {
Parser parser = new Parser();
String code = "if (a == 6) { method1(); if (a < 2) { method3(); } }if (a == 5) { method1();\n\r" +
" method2() }";
String preprocessed = parser.preprocessing(code);
List<String> extracted = parser.extract(preprocessed);
List<String> postprocessed = parser.postprocessing(extracted);
for(String ifContent: postprocessed) {
System.out.println("Line: " + ifContent);
}
}
}
Will ouptput:
Line: method1(); if (a < 2) { method3(); }
Line: method1();
method2()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Usually, a regular expression can only indicate whether the statement and the rule match. I want to be able to indicate the wrong character when I'm not, what should I do。
Pattern compile = Pattern.compile("[a-zA-Z]*");
Matcher matcher = compile.matcher("abc1ac");
I want to make an error in the character 1.
I want to check a filter,like (name eq '1' or nickname ne '2') and code like '''' and user.name like 'jo%', the value using double '' translation
I write as follow,maybe better to use design pattern.
public class FilterExpressionException extends RuntimeException {
private String expression;
private int index;
public FilterExpressionException(String expression, int index) {
super(mark(expression, index));
this.expression = expression;
this.index = index;
}
public FilterExpressionException(String message, String expression, int index) {
super(message + "(" + mark(expression, index) + ")");
this.expression = expression;
this.index = index;
}
private static String mark(String expression, int index) {
return expression.substring(0, index) + '^' + expression.substring(index);
}
public String getExpression() {
return expression;
}
public int getIndex() {
return index;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class FilterExpressionParserImpl {
private String[] relations = {"eq", "ne", "gt", "ge", "lt", "le", "like", "in", "bt"};
private String[] logices = {"and", "or"};
private char[][] relationChars = toCharArray(relations);
private char[][] logicChars = toCharArray(logices);
public static void main(String[] args) {
FilterExpressionParserImpl impl = new FilterExpressionParserImpl();
String expr = "(name eq '' or nickname ne '2' ) and (code like '''' or (user.A.name eq '''go''go''go'))";
List<String> parse = impl.parse(expr);
System.out.println(parse);
List<String> strings = impl.followUp(parse);
System.out.println(strings);
}
private class Variable {
Stack<String> words = new Stack<>();
Stack<Character> characters = new Stack<>();
Stack<Integer> brackets = new Stack<>();
int step = 0;
char character;
int commaTimes = 0;
char[][] relations;
char[][] logices;
public Variable(char[][] relations, char[][] logices) {
this.relations = relations;
this.logices = logices;
}
}
private List<String> followUp(List<String> middles) {
List<String> afters = new ArrayList<>(middles.size());
Stack<String> operators = new Stack<>();
String top;
for (String middle : middles) {
switch (middle) {
case "and":
case "or":
if (operators.size() > 0 && !operators.peek().equals("(")) afters.add(operators.pop());
operators.push(middle);
break;
case "(":
operators.push(middle);
break;
case ")":
while (!(top = operators.pop()).equals("(")) afters.add(top);
break;
default:
afters.add(middle);
}
}
while (!operators.isEmpty()) afters.add(operators.pop());
return afters;
}
private List<String> parse(String filter) {
filter = filter.trim();
Variable variable = new Variable(relationChars, logicChars);
for (int i = 0; i < filter.length(); i++) {
variable.character = filter.charAt(i);
switch (variable.character) {
case ' ':
if (variable.characters.isEmpty()) continue;
switch (variable.step) {
case 0:
dealPropertyPathEnd(filter, i, variable);
break;
case 1:
dealRelationEnd(variable);
break;
case 2:
dealValueEnd(filter, i, variable);
break;
case 3:
dealLogicEnd(variable);
break;
}
pushWord(variable);
break;
case '(':
if (variable.step != 0) throw new FilterExpressionException(filter, i);
variable.words.push(String.valueOf(variable.character));
variable.brackets.push(i);
break;
case ')':
if (variable.brackets.size() == 0) throw new FilterExpressionException(filter, i);
variable.brackets.pop();
if (variable.step == 2 && !variable.characters.isEmpty()) {
dealValueEnd(filter, i, variable);
pushWord(variable);
}
if (variable.step != 3) throw new FilterExpressionException(filter, i);
variable.words.push(String.valueOf(variable.character));
break;
default:
switch (variable.step) {
case 0:
if (!(isLetter(variable.character) || variable.character == '.')
|| ((variable.characters.size() == 0 || variable.characters.peek() == '.') && variable.character == '.'))
throw new FilterExpressionException(filter, i);
break;
case 1:
variable.relations = find(variable.relations, variable.characters.size(), variable.character);
if (variable.relations == null) throw new FilterExpressionException(filter, i);
break;
case 2:
if (variable.characters.size() == 0) {
if (variable.character != '\'') throw new FilterExpressionException(filter, i);
}
else {
if (variable.character == '\'') variable.commaTimes++;
else if (variable.commaTimes % 2 != 0) throw new FilterExpressionException(filter, i);
}
break;
case 3:
variable.logices = find(variable.logices, variable.characters.size(), variable.character);
if (variable.logices == null) throw new FilterExpressionException(filter, i);
break;
}
variable.characters.push(variable.character);
break;
}
}
if (!variable.characters.isEmpty()) {
if (variable.characters.peek() != '\'') throw new FilterExpressionException(filter, filter.length() - 1);
if (variable.commaTimes % 2 != 1) throw new FilterExpressionException(filter, filter.length() - 1 - variable.commaTimes);
pushWord(variable);
}
if (!variable.brackets.isEmpty()) throw new FilterExpressionException(filter, variable.brackets.firstElement());
return variable.words;
}
private void pushWord(Variable variable) {
String string = join(variable.characters);
if (variable.step == 3) {
if (string.equals("\'\'")) {
string = "";
} else {
string = string.replace("\'\'", "\'");
string = string.substring(1, string.length() - 1);
}
}
variable.words.push(string);
variable.characters.clear();
}
private String join(List<Character> characters) {
StringBuilder builder = new StringBuilder();
characters.forEach(builder::append);
return builder.toString();
}
private void dealPropertyPathEnd(String filter, int i, Variable variable) {
if (variable.characters.peek() == '.') throw new FilterExpressionException(filter, i);
variable.step = 1;
}
private void dealRelationEnd(Variable variable) {
variable.relations = relationChars;
variable.step = 2;
}
private void dealValueEnd(String filter, int i, Variable variable) {
if (variable.characters.peek() != '\'') throw new FilterExpressionException(filter, i);
if (variable.commaTimes % 2 != 1) throw new FilterExpressionException(filter, i);
variable.commaTimes = 0;
variable.step = 3;
}
private void dealLogicEnd(Variable variable) {
variable.logices = logicChars;
variable.step = 0;
}
private boolean isLetter(char character) {
return ('a' <= character && character <= 'z') || ('A' <= character && character <= 'Z');
}
private char[][] toCharArray(String[] strings) {
char[][] chars = new char[strings.length][];
for (int i = 0; i < strings.length; i++) {
chars[i] = strings[i].toCharArray();
}
return chars;
}
private char[][] find(char[][] sources, int column, char character) {
if (sources == null || sources.length == 0) return sources;
List<Integer> indexes = new ArrayList<>(sources.length);
for (int i = 0; i < sources.length; i++) {
if (sources[i].length > column && sources[i][column] == character) indexes.add(i);
}
if (indexes.isEmpty()) return null;
char[][] targets = new char[indexes.size()][];
for (int i = 0; i < indexes.size(); i++) {
targets[i] = sources[indexes.get(i)];
}
return targets;
}
}
Java's regular expression engine can do much more than just matching text. For more information, read the documentation.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LookingAt {
public static String validate(String input, Pattern regex) {
Matcher m = regex.matcher(input);
if (m.matches())
return "OK";
if (m.lookingAt())
return "First error at index " + m.end();
if (m.hitEnd())
return "Too short";
return "Cannot match at all";
}
public static void main(String[] args) {
System.out.println(validate("abcac", Pattern.compile("[a-zA-Z]*")));
System.out.println(validate("abc1ac", Pattern.compile("[a-zA-Z]*")));
System.out.println(validate("abcac", Pattern.compile("[a-zA-Z]{10,}")));
System.out.println(validate("1abcac", Pattern.compile("[a-zA-Z]+")));
}
}
I'm trying to write infix to postfix program in java using stack. Here is my code:
import java.io.*;
import java.util.*;
public class ONP{
public static void main(String args[]) throws java.io.IOException, NumberFormatException ,EmptyStackException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
StringBuilder out= new StringBuilder();
Stack st=new Stack();
for(int i=0;i<n;i++){
String input=br.readLine();
char in[]=input.toCharArray();
int len=input.length();
for (int j=0;j<len;j++){
if (in[j]>='a' && in[j]<='z'){
out.append(in[j]);
}
else if(in[j]=='('){
st.push(new Character(in[j]));
}
else if(in[j]=='+' || in[j]=='-' || in[j]=='*' || in[j]=='/' || in[j]=='^'){
st.push(new Character(in[j]));
}
else if(in[j]==')'){
int k=j;
while(in[k]!='(' && !st.empty() ){
char ch=st.pop().toString().charAt(0);
if(ch!='('&&ch!=')')
out.append(ch);
k--;
}
}
}
out.append("\n");
}
System.out.print(out);
}
}
Input:
((a+t)*((b+(a+c))^(c+d)))
Output:
at+bac++*cd+^
"*" should come after "+^" but it comes after "++". I can't find the bug.
It's just a small error.
In this code you look for '(' in the "in" array which makes no sense. You only want to look for it on the stack.
else if(in[j]==')'){
int k=j;
while(in[k]!='(' && !st.empty() ){
char ch=st.pop().toString().charAt(0);
if(ch!='('&&ch!=')')
out.append(ch);
k--;
}
}
Just change it to this
else if(in[j]==')'){
while(!st.empty() ){
char ch=st.pop().toString().charAt(0);
if(ch == '(') break;
out.append(ch);
}
}
That should fix the error. But there are a lot of other details you could improve. e.g. put part of the code into another method and make use of generics and autoboxing. Use Strings instead of chars to allow operators with more then one character and also implement operator precedence so you don't need so many parentheses.
Look at this.
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class ONP {
private static int precedence(String operator) {
switch(operator) {
case "(": return 0;
case "+": case "-": return 1;
case "*": case "/": return 2;
case "^": return 3;
case "sin": case "cos": return 4;
default: return -1;
}
}
private static List<String> tokenize(String input) {
ArrayList<String> tokens = new ArrayList<>();
Pattern p = Pattern.compile("\\(|\\)|[A-Za-z]+|\\d*\\.\\d*|\\d+|\\S+?");
Matcher m = p.matcher(input);
while(m.find()) {
tokens.add(m.group());
}
return tokens;
}
private static List<String> toPostfix(List<String> infix) {
Stack<String> st = new Stack<>();
ArrayList<String> out = new ArrayList<>();
for(String s: infix) {
if(s.equals("(")){
st.push(s);
} else if(s.equals(")")){
while(!st.empty() ){
String s2 = st.pop();
if(s2.equals("(")) break;
out.add(s2);
}
} else if(precedence(s) > 0) {
int p = precedence(s);
while(!st.isEmpty() && precedence(st.peek()) >= p) out.add(st.pop());
st.push(s);
} else {
out.add(s);
}
}
while(!st.isEmpty()) out.add(st.pop());
return out;
}
public static void main(String args[]) throws java.io.IOException, NumberFormatException ,EmptyStackException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++){
String input=br.readLine();
List<String> tokens = tokenize(input);
System.out.println("tokens: " + tokens);
List<String> postfix = toPostfix(tokens);
System.out.print("postfix: ");
for(String s: postfix) System.out.print(s + " ");
System.out.println();
}
}
}
Well you need to implement operator precedence, and you haven't. You need to look up the Dijkstra Shunting-yard algorithm.
I'm trying to replace the string "abc" with certain patterns:
When 'a' replace with 'b', when 'b' replace with 'c', when 'c' replace with 'a'.
The code i have right now is :
String alp = "abc";
String alteredAlp = "";
char [] charAlp = alp.toLowerCase().toCharArray();
for (int i = 0; i < charAlp.length() - 1; i++)
{
if(charAlp[i] == 'a')
alteredAlp += 'b';
else if(charAlp[i] == 'b')
alteredAlp += 'c';
else if(charAlp[i] == 'c')
alteredAlp += 'a';
}
I tried using a 'replaceAll' and I was running into a couple issue after the first iteration.
Any ideas on how to make this better?
Thanks in advance!
There is no such method as length() for a char[]. Change that to charAlp.length
Next, change charAlp.length - 1 to charAlp.length and your code will work.
Instead, you can do something like this: (with no additional String)
for (int i = 0; i < charAlp.length ; i++)
{
if(charAlp[i] == 'a')
charAlp[i]= 'b';
else if(charAlp[i] == 'b')
charAlp[i]= 'c';
else if(charAlp[i] == 'c')
charAlp[i]= 'a';
}
System.out.println(charAlp);
Now, you changed charAlp to be "bca" instead of "abc" (as you wanted)
A little more generic way:
The Alphabet must be defined without gaps!
But can also be "abcdefgh" :)
public class CharObfuscator {
public static final String ALPHABET = "abc";
public static String obfuscate(String obfuscateThis) {
char bottomLevel = ALPHABET.charAt(0); // its an 'a' (int 97)
char topLevel = ALPHABET.charAt(ALPHABET.length() - 1); // its an 'c' (int 99)
StringBuilder stringBuilder = new StringBuilder();
for (char character : obfuscateThis.toLowerCase().toCharArray()) {
if ((character - bottomLevel + 1) % (topLevel - bottomLevel + 1) != 0) {
stringBuilder.append(++character);
} else {
stringBuilder.append(bottomLevel);
}
}
return stringBuilder.toString();
}
public static void main(String[] args) {
System.out.println(obfuscate("abccbaaabbcc"));
}
}
Result:
bcaacbbbccaa
public static void main(String[] args) {
String alp = "abc";
String alteredAlp = "";
for(int i=0; i<alp.length(); i++){
if(alp.charAt(i)=='a')
alteredAlp=alteredAlp + 'b';
else if(alp.charAt(i)=='b')
alteredAlp = alteredAlp+'c';
else if(alp.charAt(i)=='c')
alteredAlp=alteredAlp+'a';
}
System.out.println(alteredAlp);
}
Try this :
No need to create character array.
Output:
bca
You can also use a StringBuilder for the same, as below:
public class CharReplace {
public static void main(String[] args) {
String alp = "abc";
StringBuilder alteredAlp = new StringBuilder();
for (int i = 0; i < alp.length(); i++) {
switch(alp.charAt(i)) {
case 'a':
alteredAlp.append('b');
break;
case 'b':
alteredAlp.append('c');
break;
case 'c':
alteredAlp.append('a');
break;
}
}
System.out.println(alteredAlp.toString());
}
}
Using a for loop, how do I go about making all consonants in a string uppercase?
I think I should do something like this:
String str = "fish$"
String strConsonants = "f, s, h";
for (int x = 0; x < str.length(); x++)
{
if(((str.charAt(x) == (strConsonants))
{
System.out.print("FiSH$");
}
}
use String.contains() method from String API. the followingcode would work for your present input. usually, if you want to find all the consonents, have an char[] of consonents or String with all the consonents and do the check.
String str = "fish$";
String strConsonants = "f, s, h";
String temp="";
for (int x = 0; x < str.length(); x++){
temp+= str.charAt(x);
if(!strConsonants.contains(temp)) {
consonentsUCase+=temp.toUpperCase();
}
temp="";
}
I've just written it.
Output: FiSH$
Works for any word ! ;)
API method: printStringWithUpperConsonant
import java.util.HashSet;
import java.util.Set;
public class ConsonantUtils {
private Set<Character> vowels = getVowels();
private String analysedString;
public ConsonantUtils(String analysedString) {
this.analysedString = analysedString;
}
public static void main(String[] args) {
new ConsonantUtils("fish$").printStringWithUpperConsonant();
}
public void printStringWithUpperConsonant() {
for (int i = 0; i < getAnalysedString().length(); i++) {
printChar(getCurrentChar(i));
}
}
private char getCurrentChar(int i) {
return getAnalysedString().charAt(i);
}
private void printChar(char currentChar) {
if (isConsonant(currentChar)) {
System.out.print(makeCharUpperCase(currentChar));
}
else{
System.out.print(currentChar);
}
}
private Set<Character> getVowels() {
Set<Character> vowels = new HashSet<Character>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
return vowels;
}
private char makeCharUpperCase(char character) {
return Character.toUpperCase(character);
}
private boolean isConsonant(char currentChar) {
return !vowels.contains(currentChar);
}
private String getAnalysedString(){
return analysedString;
}
}
You can use Character.toUpperCase().
String vowels = "aeiouAEIOU";
char[] charArr = str.toCharArray(); //turn the string into char array
for(int i=0; i<charArr.length; i++) { //for loop
if(vowels.indexOf(charArr[i]) == -1) { // if not a vowel
charArr[i] = Character.toUpperCase(charArr[i]); //replace with upper case
}
}
Sting rslt = String.valueOf(charArr); //finally turn the array back to string
String str = "fish$";
String strVowels = "aeiouAEIOU";
String out = "";
for (Character c : str.toCharArray())
{
if(!strVowels.contains(""+c))
{
out = out + Character.toUpperCase(c);
}
else
{
out = out + c;
}
}
System.out.println(out);
This works for me:
List<Character> constants = Arrays.asList('f', 's', 'h');
String str = "fish$";
for (Character c : constants) {
str = str.replace(c, Character.toUpperCase(c));
}