Reputation: 47
Okay, I am trying to add a new line every three letters. The goal is to have 20 rows of randomized capital letters. With 3 capital letters per row.
I randomized the numbers and then converted them to a string. Then outputted the string. I have 60 random capital letters in one straight String, but I can't figure out how to add new lines, since each letter is randomized I can't tell java what to search for like I did before. I have tried using an if statement and add an "/n" at the end, but it just got messy and difficult. And the operator commands I couldn't figure out how to apply every three letters either.
Here is the code I have so far under the "wrightRandomCodesToFile"
package mp05;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Main {
private static final int PRODUCT_COUNT = 20;
private static final int CHARACTERS_PER_CODE = 3;
private static int charA;
private static int preChars;
private static String preLetters;
public static void main(String[] args) {
/* (1) Generate three character alphabetic prefix codes. */
writeRandomCodesToFile("prefix.txt", 'A', 'Z',
CHARACTERS_PER_CODE, PRODUCT_COUNT);
// when you hit run, there is 3 numbers. six digits though.
/* (2) Generate three character alphabetic suffix codes. */
writeRandomCodesToFile("suffix.txt", 'A', 'Z',
CHARACTERS_PER_CODE, PRODUCT_COUNT);
/* (3) Generate three character numeric inline codes. */
writeRandomCodesToFile("inline.txt", '0', '9',
CHARACTERS_PER_CODE, PRODUCT_COUNT);
/* (4) Merge the two alphabetic and one numeric code to produce the
* final product code. */
mergeProductCodesToFile("prefix.txt", "inline.txt", "suffix.txt",
"productCode.txt");
}// end main()
/* (1),(2),(3) Generate the alphanumeric codes. These are three aphanumeric
* character combinations generated randomly with each character in the
* range A-Z | 0-9. Thus you will have codes from AAA-ZZZ | 000-999.
*
* Write numberOfCodesToGenerate codes to the specified file.
*/
public static void writeRandomCodesToFile(String codeFile,
char fromChar, char toChar,
int numberOfCharactersPerCode,
int numberOfCodesToGenerate) {
Random rnd = new Random();
charA = 65;
for (int i = 1; i <= 20; i++) {
try (
PrintWriter fout = new PrintWriter(new File("prefix.txt")); //Creates File
) {
preChars = charA + rnd.nextInt(26);
preLetters = String.valueOf((char) preChars);
System.out.print(preLetters);
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
}
}
}// end writeRandomCodesToFile()
/* (4) Merge the two alphabetic and one numeric code to produce the final
* product code. The final product code will consist of <prefix><inline><suffix>.
* Each code will be in the range AAA000AAA-ZZZ999ZZZ.
*
* Merge by using a prefix, an inline and a suffix code from their corresponding
* files. Each individual code (pre, in, post) is used exactly once, and all values are used.
*
* Write these final product codes to the file specified in productFile.
*/
public static void mergeProductCodesToFile(String prefixFile,
String inlineFile,
String suffixFile,
String productFile) {
}// end mergeProductCodesToFile()
}// end Main
Upvotes: 0
Views: 548
Reputation: 1580
import java.util.Random;
import java.io.PrintWriter;
class test{
public static String randomString(String chars, int length) {
Random rand = new Random();
StringBuilder buf = new StringBuilder();
for (int i=0; i<length; i++) {
buf.append(chars.charAt(rand.nextInt(chars.length())));
}
return buf.toString().replaceAll("...","$0\n");
}
public static void main(String[] args ) {
String myString = randomString("ABCDEFGHIJKLMONQRSTUVWXYZ",60);
try {
PrintWriter pw = new PrintWriter("fileame.txt");
pw.println(myString);
pw.close();
} catch (Exception e) {}
}
}
Upvotes: 0
Reputation: 425248
Ignoring your context and answering the question as per the title:
Given a long string, to insert a newline every 3 chars:
str = str.replaceAll("...", "$0\n");
Upvotes: 2