【问题标题】:Include a condition with an input在输入中包含条件
【发布时间】:2016-05-03 21:36:11
【问题描述】:

场景一:要求用户输入 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);
    }
}

}


对上述代码的反思。

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

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

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

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

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

提前致谢! :)

【问题讨论】:

    标签: java for-loop while-loop boolean user-input


    【解决方案1】:

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

     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()
    

    【讨论】:

    • 嘿Helliaca!这确实解决了长度问题。如何解决“询问用户是否要更改代码”然后根据答案继续更改它的问题?当前代码可以更改 3 位代码。但它不能根据用户输入关闭和打开它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2012-12-31
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多