【发布时间】:2013-07-09 03:55:14
【问题描述】:
好的,我想用特定输入替换用户返回的字符串。我想用'?'替换字符串中的每个小写字母,但不是空格!插入'?'的地方should go 在 print 语句中,末尾带有随机空格(“”)。谢谢您的帮助。
import java.util.Scanner;
public class Program05 {
public static void main(String[] args)
{
Scanner scanner01 = new Scanner(System.in);
String inputPhrase;
do {
System.out.print("Enter a common phrase to begin!: ");
inputPhrase = scanner01.nextLine();
} while (!inputPhrase.matches("[a-z ]+"));
System.out.print("Press [ENTER] to continue...");
try
{
System.in.read();
System.in.skip(System.in.available());
} catch(Exception e){e.printStackTrace();} //though that was a neat little touch
do {
boolean valid = true;
for (int i = 0; i < inputPhrase.length(); i++)
{
System.out.println("Common Phrase\n-------------" + "\n" + " " );
}
//System.out.print(inputPhrase);
} while (!inputPhrase.matches("[a-z ]+"));
}
}
【问题讨论】:
-
AFAIK 使用
Scanner或简单的阅读器是不可能的。从 Java 6 开始,您可以使用Console#readPassword来满足您的需求。 -
不要抓到
Exception!捕获实际的异常类型,如果您真的不想处理它,请将其包装在RuntimeException中并重新抛出它,而不是打印堆栈跟踪。以后你会省去一些调试上的麻烦。
标签: java string character replace