java中的重新排列字符串

问题描述 投票:0回答:1

所以我的问题是我让这样说的字符串或数组:

String a = "cola";
String b = "5000";
String k = a + " | " + b;
String BigIn = "----------";

输出是:-

----------------------
Cola | 5000
TestAnotherOne | 6000
----------------------

我需要做的是重新排列输出,就像这样:-

----------------------
Cola           | 5000
TestAnotherOne | 6000
Anotheronebut  |
BigString      | 7000
----------------------

因此|被卡在字符串中了

是否有帮助?

编辑:

我使用pring 2d来打印帐单,我用来执行此操作的类是:-

public PageFormat getPageFormat(PrinterJob pj) {

    PageFormat pf = pj.defaultPage();
    Paper paper = pf.getPaper();

    double middleHeight = 99999;
    double headerHeight = 2.0;
    double footerHeight = 2.0;
    double width = convert_CM_To_PPI(8);      //printer know only point per inch.default value is 72ppi
    double height = convert_CM_To_PPI(headerHeight + middleHeight + footerHeight);
    paper.setSize(width, height);
    paper.setImageableArea(
            0,
            2,
            width,
            height - convert_CM_To_PPI(1)
    );   //define boarder size    after that print area width is about 180 points

    pf.setOrientation(PageFormat.PORTRAIT);           //select orientation portrait or landscape but for this time portrait
    pf.setPaper(paper);

    return pf;
}

protected static double convert_CM_To_PPI(double cm) {
    return toPPI(cm * 0.393600787);
}

protected static double toPPI(double inch) {
    return inch * 72d;
}

public class BillPrintable implements Printable {

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException {

        int result = NO_SUCH_PAGE;
        if (pageIndex == 0) {

            Graphics2D g2d = (Graphics2D) graphics;

            double width = pageFormat.getImageableWidth();

            g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());

            FontMetrics metrics = g2d.getFontMetrics(new Font("TimesRoman", Font.BOLD, 7));

            int idLength = metrics.stringWidth("000");
            int amtLength = metrics.stringWidth("000000");
            int qtyLength = metrics.stringWidth("00000");
            int priceLength = metrics.stringWidth("000000");
            int prodLength = (int) width - idLength - amtLength - qtyLength - priceLength - 17;

            int productPosition = 0;
            int discountPosition = prodLength + 5;
            int pricePosition = discountPosition + idLength + 10;
            int qtyPosition = pricePosition + priceLength + 4;
            int amtPosition = qtyPosition + qtyLength;

            try {

                int y = 20;
                int yShift = 10;
                int headerRectHeight = 15;
                int headerRectHeighta = 40;

                String itiname;
                String CatToGetID;
                int couniti;
                int iticounter;
                String proptoremove;
                nf.setMaximumFractionDigits(2);

                int v = jTable1.getRowCount();
                int h = jTable1.getRowCount();

                String[] strArray = new String[v];
                String[] strCount = new String[v];
                String[] strAmount = new String[v];

                for (int x = 0; x < v; x++) {

                    proptoremove = (String) jTable1.getValueAt(x, 3);
                    proptoremove = proptoremove.replace("IQD", "");
                    proptoremove = proptoremove.replace(" ", "");

                    iticounter = Integer.parseInt((String) jTable1.getValueAt(x, 1));

                    couniti = Integer.valueOf(proptoremove);

                    itiname = (String) jTable1.getValueAt(x, 0);

                    strArray[x] = itiname;
                    strCount[x] = Integer.toString(iticounter);
                    strAmount[x] = Integer.toString(couniti);

                }

                pricetoset = Forms.OrderShow.counttoshow2.getText();
                pricetoset = pricetoset.replace("IQD", "");
                pricetoset = pricetoset.replace(" ", "");

                cpuntAll = Integer.valueOf(pricetoset);

                ///////////////// Product price Get ///////////
                int sum = 0;
                ///////////////// Product price Get   Forms.OrderShow.TNum.getText()   ///////////

                g2d.setFont(new Font("TimesRoman", Font.CENTER_BASELINE, 24));
                g2d.drawString("         - " + Forms.OrderShow.TNum.getText() + " -", 24, y);
                y += yShift;
                y += yShift;
                y += yShift;
                g2d.setFont(new Font("TimesRoman", Font.CENTER_BASELINE, 14));
                g2d.drawString("           -::: " + DashBord.sname.getText() + " :::-", 16, y);
                y += yShift;
                g2d.setFont(new Font("TimesRoman", Font.CENTER_BASELINE, 10));
                g2d.drawString("", 12, y);
                y += headerRectHeight;
                g2d.drawString("#   |    Order Name    | Quantity |  Amount", 10, y);
                y += yShift;
                g2d.drawString("--------------------------------------------------------", 10, y);
                y += headerRectHeight;
                for (int q = 0; q < h; q++) {

                    int count = strArray[q].length();

                    String passiv = "                                 ";

                    for (int l = 0; l <= count; l++) {

                        passiv = passiv.replaceFirst(" ", "");

                    }

                    String ss = String.format("%s %-20s %-20s", strArray[q], strCount[q], strAmount[q] + " IQD");

                    g2d.drawString(ss, 10, y);
                    y += yShift;
                }
                y += yShift;
                g2d.drawString("--------------------------------------------------------", 10, y);
                y += yShift;
                g2d.setFont(new Font("TimesRoman", Font.CENTER_BASELINE, 14));
                g2d.drawString("Total Amount IQD: " + cpuntAll + " IQD", 10, y);
                y += yShift;
                y += yShift;
                g2d.drawString("Total Amount USA: " + (nf.format(cpuntAll / Classes.ProgramManeInfo.USAmmount)) + " $", 10, y);
                y += yShift;
                g2d.setFont(new Font("TimesRoman", Font.CENTER_BASELINE, 10));
                g2d.drawString("--------------------------------------------------------", 10, y);
                y += yShift;
                g2d.drawString("", 10, y);
                y += yShift;
                g2d.drawString("" + "Bill Number: " + Forms.OrderShow.Bnum.getText(), 10, y);
                y += yShift;
                g2d.drawString("", 10, y);
                y += yShift;
                g2d.drawString("*************************************************", 10, y);
                y += yShift;
                g2d.drawString("    THANKS TO VISIT OUR RESTUARANT   ", 10, y);
                y += yShift;
                g2d.drawString("*************************************************", 10, y);

            } catch (NumberFormatException r) {
            }

            result = PAGE_EXISTS;
        }
        return result;
    }
}

输出是:

output

java arrays string
1个回答
0
投票

为了解决此问题,我使用了具有固定字体的另一种字体类型:-

 g2d.setFont(new Font("monospaced", Font.PLAN, 14));

代替

g2d.setFont(new Font("TimesRoman", Font.CENTER_BASELINE, 14));
© www.soinside.com 2019 - 2024. All rights reserved.