java - Adding A New Line Every Three Digits -
okay, trying add new line every 3 letters. goal have 20 rows of randomized capital letters. 3 capital letters per row.
i randomized numbers , converted them string. outputted string. have 60 random capital letters in 1 straight string, can't figure out how add new lines, since each letter randomized can't tell java search did before. have tried using if statement , add "/n" @ end, got messy , difficult. , operator commands couldn't figure out how apply every 3 letters either.
here code have far under "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 3 character alphabetic prefix codes. */ writerandomcodestofile("prefix.txt", 'a', 'z', characters_per_code, product_count); // when hit run, there 3 numbers. 6 digits though. /* (2) generate 3 character alphabetic suffix codes. */ writerandomcodestofile("suffix.txt", 'a', 'z', characters_per_code, product_count); /* (3) generate 3 character numeric inline codes. */ writerandomcodestofile("inline.txt", '0', '9', characters_per_code, product_count); /* (4) merge 2 alphabetic , 1 numeric code produce * final product code. */ mergeproductcodestofile("prefix.txt", "inline.txt", "suffix.txt", "productcode.txt"); }// end main() /* (1),(2),(3) generate alphanumeric codes. these 3 aphanumeric * character combinations generated randomly each character in * range a-z | 0-9. have codes aaa-zzz | 000-999. * * write numberofcodestogenerate codes specified file. */ public static void writerandomcodestofile(string codefile, char fromchar, char tochar, int numberofcharacterspercode, int numberofcodestogenerate) { random rnd = new random(); chara = 65; (int = 1; <= 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 2 alphabetic , 1 numeric code produce final * product code. final product code consist of <prefix><inline><suffix>. * each code in range aaa000aaa-zzz999zzz. * * merge using prefix, inline , suffix code corresponding * files. each individual code (pre, in, post) used once, , values used. * * write these final product codes file specified in productfile. */ public static void mergeproductcodestofile(string prefixfile, string inlinefile, string suffixfile, string productfile) { }// end mergeproductcodestofile() }// end main
ignoring context , answering question per title:
given long string, insert newline every 3 chars:
str = str.replaceall("...", "$0\n");
Comments
Post a Comment