0
シナリオ1:ユーザーに5桁の入力番号と3桁のコードを要求され、ファイル名とファイル内で置換されます。入力を含む条件を含める
シナリオ2:3桁のコードを入力/変更する場合は、5桁の入力番号を入力してからを入力してください。はいの場合、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);
}
}
}上記のコードで
リフレクション。
現在、両方の値を入力すると機能します。どこが間違っていますか?
さらなる拡張機能: コードの長さを確認してください。
は、私はシンプル
if (inputCode.length == 3){
}
else {
System.out.print ln ("Error")
}
行うことができます理解ししかし、イムは、ブール値とし、ループしながら、枢密院しないと、ユーザーが別の値を入力した場合、私は彼らが再びプログラムを実行することに対して再び促したいです。
ありがとうございます! :)
ちょっとHelliaca!これは長さの問題を解決します。 「コードを変更したい場合はユーザーに聞いてください」を解決し、回答に基づいて問題を変更するにはどうすればよいですか? 現在のコードは3桁のコードを変更できます。しかし、それはユーザーの入力に基づいてそれをオフにすることはできません。 – Dasman