gpt4 book ai didi

java - 在输入中包含条件

转载 作者:行者123 更新时间:2023-12-01 09:58:20 25 4
gpt4 key购买 nike

场景一:要求用户输入 5 位数字和 3 位代码,然后将其替换为文件名和文件内。

场景二:要求用户输入 5 位数字,然后询问用户是否要输入/更改 3 位数字代码。如果是,那么他们可以输入 3 位数的代码。

当前代码:

package blah blah

import all stuffs...

public class NumbChanger{

public static void main(String[] args) {
try {
Scanner user = new Scanner(System.in);
String inputCode= "";
System.out.print("Enter a xml file directory: "); // Enter xml file directory.
String directory = user.nextLine();

System.out.print("Enter the 5 digit starting Number: ");
int inputNumber = user.nextInt();

System.out.print("Do you want to change the code?");
boolean yesChange = user.hasNext();
if (!yesChange){

} else {
System.out.print("Enter the 3 character Code: ");
inputCode = user.next();
}

user.close();

Path folder = Paths.get(directory);

FilenameFilter xmlFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".xml")) {
return true;
} else {
return false;
}
}
};
//this is the list of files
File[] allFiles = folder.toFile().listFiles(xmlFilter);

if (allFiles == null) {
throw new IOException("No files found");
}

String fileName;
for(File aFile : allFiles) {

if (aFile.isFile()) {
fileName = aFile.getName();

String oldNumber = fileName.substring(
((fileName.lastIndexOf(".")) - 12), (fileName.lastIndexOf(".")) - 4);

String oldCode = fileName.substring(
((fileName.lastIndexOf(".")) - 3), (fileName.lastIndexOf(".")));

if (!yesChange){

} else {
inputCode = fileName.substring(
((fileName.lastIndexOf(".")) - 3), (fileName.lastIndexOf(".")));
}

String newNumber = String.valueOf(inputNumber++);

String newFileName = fileName.replaceAll(
oldNumber, newNumber);
if (!yesChange){

} else {
newFileName = newFileName.replaceAll(oldCode, inputCode);
}
//renaming the file
Path newFilePath = Files.move(aFile.toPath(),
aFile.toPath().resolveSibling(newFileName));

//replacing the entry # within the XML
String content = new String(Files.readAllBytes(newFilePath),
StandardCharsets.UTF_8);
content = content.replaceAll(oldNumber, newNumber);
content = content.replaceAll(oldCode, inputCode);
Files.write(newFilePath, content.getBytes(StandardCharsets.UTF_8));
}

}
System.out.print(allFiles.length + " xml files were changed.");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println(" Good Job!");
System.exit(0);
}
}

}

<小时/>

对上面代码的反射(reflection)。

目前,如果他们为两者输入值,我就可以使其工作。我哪里出错了?

进一步增强:检查代码的长度。

我知道我可以做一个简单的

if (inputCode.length == 3){
}
else {
System.out.print ln ("Error")
}

但是我不知道 boolean 值和 while 循环,如果用户输入不同的值,我希望他们再次提示,而不是再次运行程序。

提前致谢! :)

最佳答案

我不确定我是否理解你的问题,但不会

 System.out.print("Enter the 5 digit starting Number: ");
int inputNumber = user.nextInt();
while(String.valueOf(inputNumber).length() != 5) {
System.out.println("Please enter a 5 digit number.");
inputNumber = user.nextInt();
}

做这份工作吗?如果号码长度不是 5 位,系统会要求用户输入新号码。

您不能在整数上使用 .length(),因此您必须先将其转换为字符串。因此该行

String.valueOf(inputNumber).length()

关于java - 在输入中包含条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com