0

Scenario One: User is asked for 5 digit Input Number and 3 digit Code and then those are replaced in file name and inside the file.

Scenario Two: User is asked for 5 digit Input Number AND then ASKED if they want to input/change the 3 digit code. If yes then they can input a 3 digit code.

Current Code:

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 on above code.

Currently I make it work if they enter values for both. Where am I going wrong?

Further enhancements: Check the length of code.

I understand I can do a simple

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

But Im not to privy with booleans and while loops and if the user enters a different value I want them to prompt again versus having to run the program again.

thanks in advance! :)

1 Answer 1

1

Im not sure I understand your question, but wouldn't

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

do the job? If the number is not 5 digits long the user is asked to enter a new one.

You cant use .length() on an Integer, so you will have to convert it to a String first. Hence the line

String.valueOf(inputNumber).length()
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Helliaca! This does solve the length problem. How to solve the "ask user if they want to change code" and then proceed to change it problem based on the answer? Current code can change the 3 digit code. But it cant turn it off and on based on user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.