Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

public Formatter format(String format, Object... args) 

Source Link

Document

Writes a formatted string to this object's destination using the specified format string and arguments.

Usage

From source file:com.itemanalysis.psychometrics.scaling.NormalizedScore.java

public String printTable(int precision) {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    String f1 = "%10." + precision + "f";
    String f2 = "%10s";
    //      String f3="%10.4f";

    //table header
    f.format("%28s", "SCORE TABLE");
    f.format("%n");
    f.format("%45s", "=============================================");
    f.format("%n");
    f.format(f2, "Original");
    f.format("%5s", "");
    f.format(f2, "Percentile");
    f.format("%5s", "");
    f.format(f2, "Normalized");
    f.format("%n");

    f.format(f2, "Value");
    f.format("%5s", "");
    f.format(f2, "Rank");
    f.format("%5s", "");
    f.format(f2, "Score");
    f.format("%n");
    f.format("%45s", "---------------------------------------------");
    f.format("%n");

    int rs = 0;/*from  w  ww  . j  a  va 2  s  .  c  o m*/
    double value = 0.0;
    double pr = 0.0;

    Iterator<Integer> iter = normScoreTable.keySet().iterator();
    while (iter.hasNext()) {
        rs = iter.next();
        pr = prank.getPercentileRankAt(rs);
        value = normScoreTable.get(rs);
        f.format(f1, (double) rs);
        f.format("%5s", "");
        f.format(f1, pr);
        f.format("%5s", "");
        f.format(f1, value);
        f.format("%5s", "");
        f.format("%n");
    }

    f.format("%45s", "=============================================");
    f.format("%n");
    return f.toString();
}

From source file:cn.knet.showcase.demos.servletproxy.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec./*from   w  w w  . j  av  a2s . c om*/
 *
 * @param in example: name=value&foo=bar#fragment
 */
protected static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:com.itemanalysis.psychometrics.cmh.CmhTable.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    double score = 0.0;

    Iterator<Comparable<?>> iter = columnMargin.valuesIterator();
    f.format("%26s", " ");
    while (iter.hasNext()) {
        score = ((Double) iter.next()).doubleValue();
        f.format("%10.0f", score);
        f.format("%5s", " ");
    }/*from  ww  w  . j av a2 s  .  co  m*/
    f.format("%n");
    f.format("%51s", "-------------------------");

    iter = columnMargin.valuesIterator();
    int index = 0;
    while (iter.hasNext()) {
        score = ((Double) iter.next()).doubleValue();
        if (index > 1)
            f.format("%15s", "---------------");
        index++;
    }
    f.format("%n");

    f.format("%11s", "Reference: ");
    f.format("%-100s", referenceRow.toString());
    f.format("%n");
    f.format("%11s", "Focal:     ");
    f.format("%-100s", focalRow.toString());
    f.format("%n");

    return f.toString();
}

From source file:edu.cmu.tetrad.cli.AbstractAlgorithmCli.java

private String createArgsInfo() {
    Formatter fmt = new Formatter();
    if (dataFile != null) {
        fmt.format("data = %s%n", dataFile.getFileName());
    }/*  www .  j  av  a  2  s  . c  om*/
    if (excludedVariableFile != null) {
        fmt.format("exclude-variables = %s%n", excludedVariableFile.getFileName());
    }
    if (knowledgeFile != null) {
        fmt.format("knowledge = %s%n", knowledgeFile.getFileName());
    }
    fmt.format("delimiter = %s%n", Args.getDelimiterName(delimiter));
    fmt.format("verbose = %s%n", verbose);
    fmt.format("thread = %s%n", numOfThreads);
    printParameterInfos(fmt);

    printValidationInfos(fmt);

    fmt.format("out = %s%n", dirOut.getFileName().toString());
    fmt.format("output-prefix = %s%n", outputPrefix);
    fmt.format("no-validation-output = %s%n", !validationOutput);

    return fmt.toString();
}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemFitGeneralizedSX2.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    f.format("%5s", "SX2  ");
    f.format("%8.4f", getValue());
    f.format("%2s", "");
    f.format("%4d", (int) getDegreesOfFreedom());
    f.format("%2s", "");
    f.format("%1.4f", getPValue());
    return f.toString();
}

From source file:org.LexGrid.LexBIG.admin.ListSchemes.java

/**
 * Primary entry point for the program.//from  w  ww.jav a  2s . com
 * 
 * @throws Exception
 */

public void run(String[] args) throws Exception {
    synchronized (ResourceManager.instance()) {
        LexBIGService lbs = LexBIGServiceImpl.defaultInstance();
        CodingSchemeRenderingList schemes = lbs.getSupportedCodingSchemes();

        // Parse the command line ...
        CommandLine cl = null;
        Options options = getCommandOptions();
        try {
            cl = new BasicParser().parse(options, args);
        } catch (ParseException e) {
            Util.displayCommandOptions("ListExtensions", options, "ListExtensions -a", e);
            return;
        }

        if (schemes.getCodingSchemeRenderingCount() == 0)
            Util.displayMessage("No coding schemes found.");
        else {
            boolean fullRequested = cl.hasOption('f');
            if (fullRequested) {
                for (CodingSchemeRendering csr : schemes.getCodingSchemeRendering()) {
                    // Separator ...
                    Util.displayMessage("=============================");
                    CodingSchemeSummary css = csr.getCodingSchemeSummary();
                    CodingScheme cs = lbs.resolveCodingScheme(css.getCodingSchemeURI(),
                            Constructors.createCodingSchemeVersionOrTagFromVersion(css.getRepresentsVersion()));
                    Util.displayMessage(ObjectToString.toString(cs, "", 80));
                }
            } else {
                Formatter f = new Formatter();

                String format = "%-20.20s|%-10.10s|%-35.35s|%-10.10s\n";
                Object[] hSep = new Object[] { Dash20, Dash10, Dash35, Dash10 };
                f.format(format, hSep);
                f.format(format, new Object[] { "Local Name", "Version", "URN", "Tag" });
                f.format(format, hSep);
                for (CodingSchemeRendering csr : schemes.getCodingSchemeRendering()) {
                    CodingSchemeSummary css = csr.getCodingSchemeSummary();
                    // Evaluate local name
                    String localName = css.getLocalName();
                    if (localName != null && localName.length() > 20)
                        localName = localName.substring(0, 18) + ">>";
                    // Evaluate version
                    String version = css.getRepresentsVersion();
                    if (version != null && version.length() > 10)
                        version = version.substring(0, 8) + ">>";
                    // Evaluate urn
                    String urn = css.getCodingSchemeURI();
                    if (urn != null && urn.length() > 35)
                        urn = urn.substring(0, 33) + ">>";
                    // Evaluate tag(s)
                    String[] tags = csr.getRenderingDetail().getVersionTags().getTag();
                    String tag = tags.length > 0 ? tags[0] : "";
                    if (tag != null && tag.length() > 10)
                        tag = tag.substring(0, 8) + ">>";
                    // Output the first line
                    f.format(format, new Object[] { localName, version, urn, tag });
                    // Output additional tags
                    for (int i = 1; i < tags.length; i++) {
                        tag = tags[i];
                        if (tag != null && tag.length() > 10)
                            tag = tag.substring(0, 8) + ">>";
                        f.format(format, "", "", "", tag);
                    }
                    // Output separator
                    f.format(format, hSep);
                }
                Util.displayMessage(f.out().toString());
                Util.displayMessage("");
                Util.displayMessage("NOTE: >> indicates column value exceeds the available width.");
                Util.displayMessage("      Specify the '-f' option for additional detail.");
            }
        }
    }
}

From source file:org.realityforge.proxy_servlet.AbstractProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 * <p/>//from   ww w.  ja  va  2 s . com
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link java.net.URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec.
 *
 * @param in example: name=value&foo=bar#fragment
 */
private static CharSequence encodeUriQuery(final CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder sb = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < MAX_ASCII_VALUE) {
            if (ASCII_QUERY_CHARS.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {
            //not-ascii
            escape = false;
        }
        if (!escape) {
            if (null != sb) {
                sb.append(c);
            }
        } else {
            //escape
            if (null == sb) {
                final int formatLength = 5 * 3;
                sb = new StringBuilder(in.length() + formatLength);
                sb.append(in, 0, i);
                formatter = new Formatter(sb);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);
        }
    }
    return sb != null ? sb : in;
}

From source file:com.yeldi.yeldibazaar.ManageRepo.java

private void redraw() {
    try {/*from   w  w  w  .j  ava 2s.  c  o  m*/
        DB db = DB.getDB();
        repos = db.getRepos();
    } finally {
        DB.releaseDB();
    }

    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    Map<String, Object> server_line;

    for (DB.Repo repo : repos) {
        server_line = new HashMap<String, Object>();
        server_line.put("address", repo.address);
        if (repo.inuse) {
            server_line.put("inuse", R.drawable.btn_check_on);
        } else {
            server_line.put("inuse", R.drawable.btn_check_off);
        }
        if (repo.pubkey != null) {
            try {
                MessageDigest digest = MessageDigest.getInstance("SHA-1");
                digest.update(Hasher.unhex(repo.pubkey));
                byte[] fingerprint = digest.digest();
                Formatter formatter = new Formatter(new StringBuilder());
                formatter.format("%02X", fingerprint[0]);
                for (int i = 1; i < fingerprint.length; i++) {
                    formatter.format(i % 5 == 0 ? " %02X" : ":%02X", fingerprint[i]);
                }
                server_line.put("fingerprint", formatter.toString());
                formatter.close();
            } catch (Exception e) {
                Log.w("FDroid", "Unable to get certificate fingerprint.\n" + Log.getStackTraceString(e));
            }
        }
        result.add(server_line);
    }
    SimpleAdapter show_out = new SimpleAdapter(this, result, R.layout.repolisticons,
            new String[] { "address", "inuse", "fingerprint" },
            new int[] { R.id.uri, R.id.img, R.id.fingerprint });

    setListAdapter(show_out);
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MINRESmethod.java

public String printStartValues() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    double[] start = getStartValues();
    for (int i = 0; i < start.length; i++) {
        f.format("%8.4f", start[i]);
        f.format("%n");
    }//from w ww. j  a  v a 2s  .co  m
    return f.toString();
}

From source file:com.itemanalysis.psychometrics.polycor.PolychoricMaximumLikelihood.java

public String print() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    double[] rowSE = getRowThresholdStandardErrors();
    double[] colSE = getColumnThresholdStandardErrors();

    f.format("%34s", "Polychoric correlation, ML est. = ");
    f.format("%6.4f", rho);
    f.format(" (%6.4f)", variance[0][0]);
    f.format("%n");
    f.format("%41s", "Test of bivariate normality: Chisquare = ");
    f.format("%-8.4f", chiSquare);
    f.format("%6s", " df = ");
    f.format("%-6.0f", df);
    f.format("%5s", " p = ");
    f.format("%-6.4f", probChiSquare);
    f.format("%n");
    f.format("%n");
    f.format("%18s", "Row Thresholds");
    f.format("%n");
    f.format("%-15s", "Threshold");
    f.format("%-10s", "Std.Err.");
    f.format("%n");

    for (int i = 0; i < rowThresholds.length; i++) {
        f.format("%6.4f", rowThresholds[i]);
        f.format("%9s", "");
        f.format("%6.4f", rowSE[i]);
        f.format("%n");
    }//from   w ww  . java  2  s  .c  o  m

    f.format("%n");
    f.format("%n");
    f.format("%19s", "Column Thresholds");
    f.format("%n");
    f.format("%-15s", "Threshold");
    f.format("%-10s", "Std.Err.");
    f.format("%n");

    for (int i = 0; i < columnThresholds.length; i++) {
        f.format("% 6.4f", columnThresholds[i]);
        f.format("%9s", "");
        f.format("% 6.4f", colSE[i]);
        f.format("%n");
    }

    f.format("%n");
    return f.toString();

}