The user enters the formula in the form of a String consisting of letters and symbols- and + for example:
String s = "b+c-a+b"; If a=4 b=5 c=2 , then how to make it easier for the specified String to become respectively s = "5+2-4+5"; ?
HashMap<String, String> map = new HashMap<String, String>();; String s = "b+c-a+b"; ArrayList<String> arraylist2 = new ArrayList<String>( Arrays.asList("4","5", "2")); ArrayList<String> arraylist1 = new ArrayList<String>( Arrays.asList("a","b", "c")); char cInput; for (int k = 0; k<arraylist1.size(); k++){ map.put(arraylist1.get(k), arraylist2.get(k)); } String res = ""; for(int i=0; i<s.length(); i++){ cInput = s.charAt(i); String tmp = Character.toString(cInput); if (map.containsKey(tmp)) tmp = map.get(tmp); res += tmp; System.out.println(res); } System.out.println(res); Result: 5 + 2-4 + 5