Hello Everyone, this is my first post on these forums, mostly because I never thought I would get this stumped ever and never had to really post on a Java forum before. So, to try and push myself harder in the Java world, I wanted to make a program that found all possible combinations of characters. I thought it would be kind of cool. I got it mostly, but it turned out to be a lot harder than I suspected ever. I commented it out to help anyone that might be reading this. Basically, what it does so far, is for each for loop added, it goes up a character in what it generates. Not how I intended it to work at all. What I want it to do is change the length based on a variable called Length. I've been working more then 2 hours trying to solve this issue, to no avail
Here's my current code that I'm using:
What it does is it actively writes the results to a file on my Desktop called test.txt.
I current have 3 for loops set on it, which by the dumb principles on my programming, means a 3 char limit.
As you can see, it's rediculasly long and hard to change the length. Does anyone have any idea how to simplify this into say, one for loop able to be changed on a variable? What would I have to do? I'm very completely stumped.
Here's my current code that I'm using:
What it does is it actively writes the results to a file on my Desktop called test.txt.
I current have 3 for loops set on it, which by the dumb principles on my programming, means a 3 char limit.
Code:
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class Generate { public static void Gen() throws IOException { //opens a file writing stream FileOutputStream fos = new FileOutputStream("C:\\Users\\Ryan\\Desktop\\test.txt"); OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8"); //the character list I'm using String CharList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //gets a list of characters String[] CharSplit = CharList.split(""); String Str = ""; //generates text document on my desktop of possible combinations //only currently a max length of 3 since I have no idea how to simulate more for loops or what not //notice that 3 for loops = char length of 3 for(int I = 1;I<CharSplit.length;I++) { //takes up first character Str += CharSplit[I]; //writes it to a file, along with a separation to the next line out.write(Str + System.getProperty("line.separator")); //clears Str for further use Str = ""; for(int O = 1;O<CharSplit.length;O++) { //writes 2 pieces into Str for a 2 char length Str += CharSplit[I] + CharSplit[O]; //writes to file along with a new line out.write(Str + System.getProperty("line.separator")); //clears Str Str = ""; for(int P = 1;P<CharSplit.length;P++) { //writes 3 pieces into Str for a 3 char length Str += CharSplit[I] + CharSplit[O] + CharSplit[P]; //writes to file along with new line out.write(Str + System.getProperty("line.separator")); //clears Str Str = ""; } } } //closes the file writing stream out.close(); } } /* How can I simplify the for loops so that they are more customizable? Ex: A variable called Length controls the maximum length, no need to paste in new for loops */
Comment