好的,我试图每三个字母添加一个新行。目标是拥有20行随机大写字母。每行3个大写字母。
我将数字随机化,然后将它们转换为字符串。然后输出字符串。我在一个直的字符串中有60个随机大写字母,但我无法弄清楚如何添加新行,因为每个字母都是随机的,我不能像以前那样告诉java搜索什么。我已经尝试使用if语句并在最后添加一个“/ n”,但它只是变得混乱和困难。操作员命令我无法弄清楚如何应用每三个字母。
以下是我目前在“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
答案 0 :(得分:2)
忽略您的背景并按照标题回答问题:
给定一个长字符串,每3个字符插入一个换行符:
str = str.replaceAll("...", "$0\n");
答案 1 :(得分:0)
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) {}
}
}