简体   繁体   中英

Using a for-loop and PrintWriter, how do I create multiple randomly named files each that have a set of their own code in them?

Basically the issue is after compiling running and entering an input. Only one file is being created. Everything is working properly except for when I enter a number larger than one. Entering a number greater than one should allow me to have multiple files each with random names being generated right from what I understand, right? Instead it chooses to only create one file with a random name and does everything normally, except for create those few extra files I told it to.

This is for personal entertainment and I am using notepad++ to create this then I am compiling and running it using javac and java in the command prompt. I am basically using what we learned in class a couple days ago to create this plus a lot of extra things that I had to do research on but I really want to find out how to do this and couldn't find much on doing it in a for-loop which is how I want to do it.

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class rangen {

    static String getnumlet(int n) {

        String numlet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr" +
            "stuvwxyz0123456789";
        StringBuilder sb = new StringBuilder(n);

        for (int i = 0 ; i < n ; i++) {
            int index = (int)(numlet.length() * Math.random());

            sb.append(numlet.charAt(index));
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        final int length = 10;
        int amount = 0;

        String printername = (rangen.getnumlet(length) + ".java");
        String name = (printername.substring(0 ,
            printername.indexOf('.')).trim());
        Scanner input = new Scanner(System.in);
        PrintWriter tofile = null;

        System.out.print("Enter the amount of files you want to create: ");
        amount = input.nextInt();

        for (int i = 1 ; i <= amount ; i++) {

            try {

                tofile = new PrintWriter(printername);

                tofile.print("// Creator: Unknown...\n");
                tofile.print("// Date Created: 9-28-2019\n");
                tofile.print("// Last Modified: 9-28-2019\n");
                tofile.print("// Description: This file was \n" + 
                    "//     randomly generated.\n");
                tofile.print("public class " + name + " {\n");
                tofile.print("  public static void main(String[] args) {\n");
                tofile.print("      private static final double inf = \n" +
                    "           Double.POSITIVE_INFINITY;\n");
                tofile.print("      for (int i = 1 ; i <= inf ; i++) {\n");
                tofile.print("          System.out.print(\"yeet \" + i);\n");
                tofile.print("      }\n");
                tofile.print("  }\n");
                tofile.print("}\n");
                for (int x = 0 ; x <= 5; x++ ) {

                    tofile.print("// LOL " + x + "\n");
                }
            }
            catch (FileNotFoundException e) {
                System.out.println("couldn't create " + printername);
            }
            tofile.close();
        }
    }
}

Let's say you put in the value of 3 in the console. Then I expect it to create three separate files each having the code in it that I told it to write to the file. No error messages should occur but only one file is being made for me instead of 3.

You need to generate a new random file name each iteration of the for loop. As written your program generates a single random file name and uses the same name each iteration.

for (int i = 1 ; i <= amount ; i++) {
    String printername = (rangen.getnumlet(length) + ".java");
    String name = printername.substring(0, printername.indexOf('.')).trim();

    try {
        tofile = new PrintWriter(printername);
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM