此程序的目标是提示用户输入单个字符和短语,然后使用' $'替换该短语中该字符的任何实例。我的程序就是这样做的,但是当我向我的教授展示时,我被告知我不能在我构建的方法中使用.replace,所以我必须找到一种不使用它的方法。我已经工作了一段时间,到目前为止我知道我可以用for循环替换它,但经过几次令人沮丧的迭代之后,我似乎无法做到这一点。对不起,如果我的代码看起来很时髦,我仍然是一名入门的java学生,所以我还在学习基础知识。我在下面的代码片段末尾提供了一个建议的解决方案。
public static char getKeyCharacter(String userInput) {
char keyCharacter;
Scanner inputStream = new Scanner(System.in);
while(userInput.length() > 1)
{
System.out.println("Please enter a SINGLE character to use as key: ");
userInput = inputStream.nextLine();
}
keyCharacter = userInput.charAt(0);
return keyCharacter;
}
public static String getString(String userResponse) {
Scanner inputStream = new Scanner(System.in);
String theString;
while(userResponse.length() > 500) {
System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
userResponse = inputStream.nextLine();
}
while(userResponse.length() < 4) {
System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
userResponse = inputStream.nextLine();
}
theString = userResponse;
return theString;
}
public static String maskCharacter(String theString, char keyCharacter){
String maskedString = "";
final char mask = '$';
maskedString = maskedString + theString.replace(keyCharacter, mask);
System.out.println("String with " + keyCharacter + " masked: ");
return maskedString;
}
public static String removeCharacter(String theString, char keyCharacter) {
String modifiedString = " ";
final char replaceChar = ' ';
modifiedString = modifiedString + theString.replace(keyCharacter, replaceChar);
System.out.println("String with " + keyCharacter + " removed:");
return modifiedString;
}
public static int countKey(String theString, char keyCharacter) {
int charCount = 0;
for (int c = 0; c < theString.length(); c++) {
if (theString.charAt(c) == keyCharacter) {
charCount++;
}
}
System.out.println("Occurences of " + keyCharacter + " in string:");
return charCount;
}
}
我相信解决方案会看起来像这样,但到目前为止,我已经不成功了 -
public static String maskCharacter(String theString, char keyCharacter){
String maskedString = "";
final char mask = '$';
for (int k = 0; k < theString.length(); k++) {
if (theString.charAt(k) == keyCharacter) {
keyCharacter = mask;
}
System.out.println("String with " + keyCharacter + " masked: ");
return maskedString;
}
我的问题在于使用mask替换所有keyCharacters,使maskedString = theString。为了记录,我还没有学习任何关于那些花哨的数组,所以如果有一种方法可以使用一个简单的for循环来做到这一点我会非常感激。感谢您提前获得帮助!
答案 0 :(得分:4)
我会使用StringBuilder
和String#toCharArray()
使用简单的for-each
循环。像,
public static String maskCharacter(String theString, char keyCharacter){
StringBuilder sb = new StringBuilder();
for (char ch : theString.toCharArray()) {
if (ch == keyCharacter) {
sb.append('$'); // <-- mask keyCharacter(s).
} else {
sb.append(ch); // <-- it isn't the character to mask
}
}
return sb.toString();
}
答案 1 :(得分:3)
我不会使用StringBuilder
:直接使用toCharArray()
的结果:
char[] cs = theString.toCharArray();
for (int i = 0; i < cs.length; ++i) {
if (cs[i] == keyCharacter) cs[i] = '$';
}
return new String(cs);
不仅更简洁,而且:
答案 2 :(得分:1)
public static String maskCharacter(String theString,char keyCharacter){
String masked = "";
for (int i = 0 ; i < theString.length() ; i++) {
if (theString.charAt(i) == keyCharacter) {
masked += "$";
}
else {
masked+=theString.charAt(i)+"";
}
}
return masked;
}
答案 3 :(得分:0)
仅使用字符串连接和基本字符访问的答案。
您似乎知道可以将某些内容连接到字符串并获取不同的字符串。
maskedString = maskedString + ...;
您还知道可以构建一个使用.charAt()
for (int k = 0; k < theString.length(); k++) {
char nch = theString.charAt(k);
}
您可以检查字符之间的相等性
if (nch == keyCharacter)
...假设你知道其他分支,是不是很清楚你只需要把它们放在一起?
if (nch == keyCharacter) {
// append '$' to maskedString
}
else {
// append nch to maskedString
}
当然,这会在每次循环迭代时创建一个新字符串,因此效率不高。但我并不认为这是演习的重点。