Example usage for java.lang String compareTo

List of usage examples for java.lang String compareTo

Introduction

In this page you can find the example usage for java.lang String compareTo.

Prototype

public int compareTo(String anotherString) 

Source Link

Document

Compares two strings lexicographically.

Usage

From source file:ListActionsJEditorPane.java

public static void main(String args[]) {
    JTextComponent component = new JEditorPane();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }//from   w w w . j a va  2  s .c om
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("acowa");
    list.add("cow");
    list.add("aow");
    Collections.sort(list, new Comparator<String>() {

        @Override/* w ww  .  j  a  v  a 2 s .  c om*/
        public int compare(String o1, String o2) {
            if (o1.length() > o2.length()) {
                return 1;
            } else {
                return o1.compareTo(o2);
            }
        }
    });

    System.out.println(list);
}

From source file:com.googlecode.dex2jar.v3.Main.java

public static void main(String... args) {
    System.err.println("this cmd is deprecated, use the d2j-dex2jar if possible");
    System.out.println("dex2jar version: translator-" + Main.class.getPackage().getImplementationVersion());
    if (args.length == 0) {
        System.err.println("dex2jar file1.dexORapk file2.dexORapk ...");
        return;//  ww w . ja  v  a2s .c  o  m
    }
    String jreVersion = System.getProperty("java.specification.version");
    if (jreVersion.compareTo("1.6") < 0) {
        System.err.println("A JRE version >=1.6 is required");
        return;
    }

    boolean containsError = false;

    for (String file : args) {
        File dex = new File(file);
        final File gen = new File(dex.getParentFile(), FilenameUtils.getBaseName(file) + "_dex2jar.jar");
        System.out.println("dex2jar " + dex + " -> " + gen);
        try {
            doFile(dex, gen);
        } catch (Exception e) {
            containsError = true;
            niceExceptionMessage(new DexException(e, "while process file: [%s]", dex), 0);
        }
    }
    System.out.println("Done.");
    System.exit(containsError ? -1 : 0);
}

From source file:TreeSetTest.java

public static void main(String[] args) {
    SortedSet<Item> parts = new TreeSet<Item>();
    parts.add(new Item("Toaster", 1234));
    parts.add(new Item("Widget", 4562));
    parts.add(new Item("Modem", 9912));
    System.out.println(parts);//from   w  w  w  . j a v  a  2 s  . c  o m

    SortedSet<Item> sortByDescription = new TreeSet<Item>(new Comparator<Item>() {
        public int compare(Item a, Item b) {
            String descrA = a.getDescription();
            String descrB = b.getDescription();
            return descrA.compareTo(descrB);
        }
    });

    sortByDescription.addAll(parts);
    System.out.println(sortByDescription);
}

From source file:Main.java

public static void main(final String[] args) {
    final List<String> asu = new ArrayList<String>();
    asu.add("2");
    asu.add("11");
    asu.add("7");
    asu.add("10");
    asu.add("7");
    asu.add("12");
    asu.add("2");
    asu.add("11");
    asu.add("11");
    asu.add("7");
    asu.add("7");
    asu.add("7");

    List<String> list = new ArrayList<String>();
    Map<String, Integer> counts = new HashMap<String, Integer>();
    list.addAll(asu);/*from  w w w .ja va2  s .  c o m*/
    for (String item : list) {
        Integer count = counts.get(item);
        if (count == null) {
            count = 1;
        } else {
            count = count + 1;
        }
        counts.put(item, count);
    }
    Collections.sort(asu, new Comparator<String>() {
        @Override
        public int compare(final String left, final String right) {
            int result = counts.get(left).compareTo(counts.get(right));
            if (result == 0) {
                result = left.compareTo(right);
            }
            return result;
        }
    });
    System.out.println(asu);
}

From source file:Main.java

public static void main(String[] args) {
    String apple = new String("Apple");
    String orange = new String("Orange");
    System.out.println(apple.equals(orange));
    System.out.println(apple.equals(apple));
    System.out.println(apple == apple);
    System.out.println(apple == orange);
    System.out.println(apple.compareTo(apple));
    System.out.println(apple.compareTo(orange));
}

From source file:SequenceStrings.java

public static void main(String[] args) {
    String string1 = "A";
    String string2 = "To";
    String string3 = "Z";

    String string1Out = "\"" + string1 + "\""; // string1 with quotes
    String string2Out = "\"" + string2 + "\""; // string2 with quotes
    String string3Out = "\"" + string3 + "\""; // string3 with quotes

    if (string1.compareTo(string3) < 0)
        System.out.println(string1Out + " is less than " + string3Out);
    else {/*from  ww w.j a v  a 2  s  . co m*/
        if (string1.compareTo(string3) > 0)
            System.out.println(string1Out + " is greater than " + string3Out);
        else
            System.out.println(string1Out + " is equal to " + string3Out);
    }

    if (string2.compareTo(string1) < 0)
        System.out.println(string2Out + " is less than " + string1Out);
    else {
        if (string2.compareTo(string1) > 0)
            System.out.println(string2Out + " is greater than " + string1Out);
        else
            System.out.println(string2Out + " is equal to " + string1Out);
    }
}

From source file:com.amazonaws.services.dynamodbv2.online.index.ViolationDetector.java

public static void main(String[] args) {
    CommandLine commandLine;/* ww  w  . j  a v a 2  s  .c o  m*/
    org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();

    Option optionHelp = new Option("h", "help", false, "Help and usage information");

    OptionBuilder.withArgName("keep/delete");
    OptionBuilder.withLongOpt("detect");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(
            "Detect violations on given table. " + "\nwith 'keep', violations will be kept and recorded;"
                    + "\nwith 'delete', violations will be deleted and recorded.");
    Option optionDetection = OptionBuilder.create("t");

    OptionBuilder.withArgName("update/delete");
    OptionBuilder.withLongOpt("correct");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("Correct violations based on records on correction input file."
            + "\nwith 'delete', records on input file will be deleted from the table;"
            + "\nwith 'update', records on input file will be updated to the table.");
    Option optionCorrection = OptionBuilder.create("c");

    OptionBuilder.withArgName("configFilePath");
    OptionBuilder.withLongOpt("configFilePath");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(
            "Path of the config file. \nThis option is required for both detection and correction.");
    Option optionConfigFilePath = OptionBuilder.create("p");

    options.addOption(optionConfigFilePath);
    options.addOption(optionDetection);
    options.addOption(optionCorrection);
    options.addOption(optionHelp);

    try {
        ViolationDetector detector = new ViolationDetector();
        commandLine = parser.parse(options, args);

        /** Violation detection */
        if (commandLine.hasOption("t")) {
            if (!commandLine.hasOption("p")) {
                logger.error("Config file path not provided. Exiting...");
                formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
                System.exit(1);
            }
            String configFilePath = commandLine.getOptionValue("p");
            detector.setConfigFile(configFilePath);

            String detectOption = commandLine.getOptionValue("t");
            if (detectOption.compareTo("delete") == 0) {
                confirmDelete();
                detector.initDetection();
                detector.violationDetection(true);
            } else if (detectOption.compareTo("keep") == 0) {
                detector.initDetection();
                detector.violationDetection(false);
            } else {
                String errMessage = "Invalid options " + detectOption + " for 't/detect'";
                logger.error(errMessage + ". Exiting...");
                formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
                System.exit(1);
            }
            return;
        }

        /** Violation correction */
        if (commandLine.hasOption("c")) {
            if (!commandLine.hasOption("p")) {
                logger.error("Config file path not provided. Exiting...");
                formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
                System.exit(1);
            }
            String configFilePath = commandLine.getOptionValue("p");
            detector.setConfigFile(configFilePath);

            String correctOption = commandLine.getOptionValue("c");
            if (correctOption.compareTo("delete") == 0) {
                confirmDelete();
                detector.initCorrection();
                detector.violationCorrection(true, false /* useConditionalUpdate */);
            } else if (correctOption.compareTo("update") == 0) {
                detector.initCorrection();
                boolean useConditionalUpdate = getUseConditionalUpdateOptionFromConsole();
                detector.violationCorrection(false, useConditionalUpdate);
            } else {
                String errMessage = "Invalid options " + correctOption + " for 'c/correct'";
                logger.error(errMessage + ". Exiting...");
                formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
                System.exit(1);
            }
            return;
        }

        /** Help information */
        if (commandLine.hasOption("h")) {
            formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
            return;
        }

        /** Error: print usage and exit */
        String errMessage = "Invalid options, check usage";
        logger.error(errMessage + ". Exiting...");
        formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
        System.exit(1);

    } catch (IllegalArgumentException iae) {
        logger.error("Exception!", iae);
        System.exit(1);
    } catch (ParseException e) {
        logger.error("Exception!", e);
        formatter.printHelp(TOOL_USAGE_WIDTH, TOOL_USAGE, null /*header*/, options, null /*footer*/);
        System.exit(1);
    }
}

From source file:com.zimbra.cs.util.GalSyncAccountUtil.java

public static void main(String[] args) throws Exception {
    if (args.length < 1)
        usage();//from   w  ww .  j  a v  a2  s.co m
    CliUtil.toolSetup();
    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    options.addOption("a", "account", true, "gal sync account name");
    options.addOption("i", "id", true, "gal sync account id");
    options.addOption("n", "name", true, "datasource name");
    options.addOption("d", "did", true, "datasource id");
    options.addOption("x", "domain", true, "for domain gal sync account");
    options.addOption("f", "folder", true, "folder id");
    options.addOption("p", "polling", true, "polling interval");
    options.addOption("t", "type", true, "gal type");
    options.addOption("s", "server", true, "mailhost");
    options.addOption("h", "help", true, "help");
    CommandLine cl = null;
    boolean err = false;
    try {
        cl = parser.parse(options, args, false);
    } catch (ParseException pe) {
        System.out.println("error: " + pe.getMessage());
        err = true;
    }

    GalSyncAccountUtil cli = new GalSyncAccountUtil();
    if (err || cl.hasOption('h')) {
        usage();
    }
    if (cl.hasOption('i'))
        cli.setAccountId(cl.getOptionValue('i'));
    if (cl.hasOption('a'))
        cli.setAccountName(cl.getOptionValue('a'));
    if (cl.hasOption('n'))
        cli.setDataSourceName(cl.getOptionValue('n'));
    if (cl.hasOption('d'))
        cli.setDataSourceId(cl.getOptionValue('d'));
    setup();
    int cmd = lookupCmd(args[0]);
    try {
        switch (cmd) {
        case TRICKLE_SYNC:
            cli.syncGalAccount();
            break;
        case FULL_SYNC:
            cli.setFullSync();
            cli.syncGalAccount();
            break;
        case FORCE_SYNC:
            cli.setForceSync();
            cli.syncGalAccount();
            break;
        case CREATE_ACCOUNT:
            String acctName = cl.getOptionValue('a');
            String dsName = cl.getOptionValue('n');
            String domain = cl.getOptionValue('x');
            String type = cl.getOptionValue('t');
            String folderName = cl.getOptionValue('f');
            String pollingInterval = cl.getOptionValue('p');
            String mailHost = cl.getOptionValue('s');
            if (acctName == null || mailHost == null || dsName == null || type == null
                    || type.compareTo("zimbra") != 0 && type.compareTo("ldap") != 0)
                usage();
            for (Element account : cli
                    .createGalSyncAccount(acctName, dsName, domain, type, folderName, pollingInterval, mailHost)
                    .listElements(AdminConstants.A_ACCOUNT))
                System.out.println(account.getAttribute(AdminConstants.A_NAME) + "\t"
                        + account.getAttribute(AdminConstants.A_ID));
            break;
        case ADD_DATASOURCE:
            acctName = cl.getOptionValue('a');
            dsName = cl.getOptionValue('n');
            domain = cl.getOptionValue('x');
            type = cl.getOptionValue('t');
            folderName = cl.getOptionValue('f');
            pollingInterval = cl.getOptionValue('p');
            if (acctName == null || dsName == null || type == null
                    || type.compareTo("zimbra") != 0 && type.compareTo("ldap") != 0)
                usage();
            for (Element account : cli
                    .addGalSyncDataSource(acctName, dsName, domain, type, folderName, pollingInterval)
                    .listElements(AdminConstants.A_ACCOUNT))
                System.out.println(account.getAttribute(AdminConstants.A_NAME) + "\t"
                        + account.getAttribute(AdminConstants.A_ID));
            break;
        case DELETE_ACCOUNT:
            String name = cl.getOptionValue('a');
            String id = cl.getOptionValue('i');
            if (name == null && id == null)
                usage();
            cli.deleteGalSyncAccount(name, id);
            break;
        default:
            usage();
        }
    } catch (ServiceException se) {
        System.out.println("Error: " + se.getMessage());
    }
}

From source file:com.mindcognition.mindraider.MindRaiderApplication.java

/**
 * The main procedure./* w w w  .  j ava  2 s . co m*/
 * 
 * @param args
 *            program arguments.
 */
public static void main(String[] args) {
    boolean help = false;
    boolean debugMode = false;

    // analyze arguments
    if (args != null) {
        // while there are some arguments try to consume them
        int i = 0;
        while (i < args.length) {
            if (ARG_HELP.equals(args[i]) || "--help".equals(args[i]) //$NON-NLS-1$
                    || "/h".equals(args[i]) || "/?".equals(args[i])) { //$NON-NLS-1$ //$NON-NLS-2$
                help = true;
            } else {
                if (ARG_PROFILE.equals(args[i])) {
                    // take next arg - there is path to the profile
                    i++;
                    MindRaider.profilesDirectory = args[i];
                } else {
                    if (ARG_TWIKI_IMPORT.equals(args[i])) {
                        // take next arg - there is path to the twiki file
                        // to be imported
                        i++;
                        String command = args[i];
                        if (StringUtils.isNotEmpty(command)) {
                            new Commander(COMMAND_TWIKI_IMPORT + command);
                        } else {
                            System.err.println(MindRaiderApplication.getString("MindRaiderApplication.0") //$NON-NLS-1$
                                    + command + MindRaiderApplication.getString("MindRaiderApplication.1")); //$NON-NLS-1$
                        }
                        System.exit(0);
                    } else {
                        if (ARG_DEBUG.equals(args[i])) {
                            debugMode = true;
                        } else {
                            System.out.println(
                                    MindRaiderApplication.getString("MindRaiderApplication.11") + args[i]); //$NON-NLS-1$
                            help = true;
                        }
                    }
                }
            }
            i++;
        }

        // process arguments
        if (help) {
            help();
            return;
        }
    }

    // reset log4j configuration
    try {
        BasicConfigurator.resetConfiguration();
        if (debugMode) {
            PropertyConfigurator.configure(System.getProperty("log4j.configuration.debug")); //$NON-NLS-1$
        } else {
            String log4jconfig = System.getProperty("log4j.configuration");
            PropertyConfigurator.configure(log4jconfig); //$NON-NLS-1$
        }
    } catch (Throwable e) {
        logger.debug(getString(MindRaiderApplication.getString("MindRaiderApplication.2"))); //$NON-NLS-1$
        BasicConfigurator.resetConfiguration();
    }

    logger.debug(MindRaider.getTitle());

    String javaVersion;
    try {
        javaVersion = System.getProperty("java.version"); //$NON-NLS-1$
    } catch (NullPointerException e) {
        javaVersion = ""; //$NON-NLS-1$
    }

    logger.debug(getString("MindRaiderApplication.javaVersion",
            new Object[] { javaVersion, System.getProperty("java.vendor"), System.getProperty("java.home") }));
    if (javaVersion.compareTo("1.1.2") < 0) {
        logger.debug(getString("MindRaiderApplication.swing112version"));
        return;
    }

    // initialization
    if (MindRaiderConstants.EARLY_ACCESS) {
        MindRaider.setUser(System.getProperty("user.name"),
                System.getProperty("user.home") + File.separator + MindRaiderConstants.MR + "-eap"); //$NON-NLS-1$
    } else {
        MindRaider.setUser(System.getProperty("user.name"), System //$NON-NLS-1$
                .getProperty("user.home")); //$NON-NLS-1$
    }

    MindRaider.setInstallationDirectory(System.getProperty("user.dir")); //$NON-NLS-1$

    logger.debug(getString("MindRaiderApplication.installationDirectory", MindRaider.installationDirectory));
    logger.debug(getString("MindRaiderApplication.profileName", MindRaider.user.getName()));
    logger.debug(getString("MindRaiderApplication.userHome", MindRaider.user.getHome()));

    MindRaider.eapProfilesDirectory = System.getProperty("user.home") + File.separator + MindRaiderConstants.MR
            + "-eap" + File.separator + ".mindraider.profile.eap";

    // set profile
    if (MindRaider.profilesDirectory == null) {
        if (MindRaiderConstants.EARLY_ACCESS) {
            MindRaider.profilesDirectory = MindRaider.eapProfilesDirectory;
        } else {
            MindRaider.profilesDirectory = MindRaider.user.getHome() + File.separator + ".mindraider.profile"; //$NON-NLS-1$
        }
    }
    MindRaider.setMainJFrame(MindRaiderMainWindow.getInstance());
}