首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每三位数加一条新行

每三位数加一条新行
EN

Stack Overflow用户
提问于 2015-07-14 00:26:35
回答 1查看 518关注 0票数 0

好吧,我试着每三个字母加一行。目标是有20行随机大写字母。每行3个大写字母。

我把数字随机化,然后把它们转换成一个字符串。然后输出字符串。我有60个随机大写字母在一个直线,但我不知道如何添加新的行,因为每个字母是随机的,我不能告诉java搜索什么,就像我以前做的那样。我尝试使用if语句,并在末尾添加"/n“,但它只是变得混乱和困难。而操作符命令,我也不知道如何应用每三个字母。

下面是我到目前为止在"wrightRandomCodesToFile“下的代码

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-14 00:37:37

忽略上下文,按照标题回答问题:

给定一个长字符串,每3个字符插入一个换行符:

代码语言:javascript
复制
str = str.replaceAll("...", "$0\n");
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31395676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档