private void replaceMethed() {
Scanner s = new Scanner(System.in);
System.out.println("===欢迎使用===");
System.out.print("请输入一串字符串:");
String str = s.next();
while (true) {
System.out.print("请输入上面的字符串的一个字串:");
String subStr = s.next();
int index = str.indexOf(subStr);
// 只要索引不为-1,就是后面输入的字符串是前面字符串的一个字串
if (index != -1) {
// 字符串替换,用"hello"替换subStr
str = str.replaceAll(subStr, "hello");
System.out.println("替换后的字符串为:" + str);
break;
} else {
System.out.println("您输入的字符串有误,请重新输入!");
}
}
}

2、BufferedReader输入

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String startTime = null; // 输入的上机时间
Date startDate = null; // 格式转换后的上机时间
while (true) {
try {
System.out.print("请输入上机时间(hh:mm格式):");
startTime = bf.readLine();
startDate = sdf.parse(startTime);
//如果没有输出就跳出循环
break;
} catch (ParseException e) {
System.out.println("格式错误,请重新上机时间(hh:mm格式):");
}