You have to log in to edit pages.
Fill the table below with the information required. It is important to write correctly the keywords and the categories, so that your article will be easy to find to the other users. We also recommend to click on watch this page so you will be notified every time another user modifies your tutorial page.
Click on the help buttons close to each field if you need help in filling them.
Java Advanced Java Basics Java EE Java SE Input Output Ajax Algorithms Application Servers Cryptography Databases Hibernate Debugging J2EE Debugging Design Patterns EJB Exceptions GUI Swing Loops Multithreading Networking Reflection Spring SQL Struts WebServices XDoclet XML XSLT XPath Methods Files Generics Servlets Collections Best practices Java5
Category : Java Advanced Java Basics Java EE Java SE Input Output Ajax Algorithms Application Servers Cryptography Databases Hibernate Debugging J2EE Debugging Design Patterns EJB Exceptions GUI Swing Loops Multithreading Networking Reflection Spring SQL Struts WebServices XDoclet XML XSLT XPath Methods Files Generics Servlets Collections Best practices Java5
Free text: This example gets from command line a <code>String</code> containing some text and finds if there's any duplicate character in it. == the article == {{versioned code|url=https://svn2.hosted-projects.com/wikijava/articles/dongiulio/SVN_LOCAL/org/wikijava/basic/hasDuplicates.java}} The core part of this example is the has duplicate method: <source lang="java"> private static boolean hasDuplicates(String string) { for (int i = 0; i < string.length() - 1; i++) { if (string.substring(i + 1).contains(string.subSequence(i, i + 1))) { return true; } } return false; }</source> which contains a for loop to check for each character of the String if the rest of the String contains that character. There are two simple optimizations in it: * The <code>for</code> doesn't check the last character of the string, it's not necessary, since there's no rest of the string * the <code>if</code> checks only for duplicates in the string starting from the character following the current character. If in the characters before this there were no duplicates there's no point in checking them again. The important methods used here are: * <code>String java.lang.String.substring(int beginIndex)</code> that returns the end of the string starting at the position beginIndex. *<code>boolean java.lang.String.contains(CharSequence s)</code> which checks if the substring, obtained with the previous method, contains one of the characters in the <code>java.lang.CharSequence</code>. *<code>CharSequence java.lang.String.subSequence(int beginIndex, int endIndex)</code> which extracts a substring and returns it as a charSequence. == hasDuplicates.java == <source lang="Java">package org.wikijava.basic; /** * @author Giulio */ public class hasDuplicates { /** * calls the hasDuplicates Method * * @param args * one param, the string to validate for duplicates */ public static void main(String[] args) { if (args.length < 1) { System.err.println("usage: hasDuplicates <string>"); return; } String string = args[0].trim(); if (hasDuplicates(string)) { System.out.println("there's a duplicate"); } return; } /** * * finds if a string contains duplicate characters * * @param string * @return true if there's a character which is duplicate in the string */ private static boolean hasDuplicates(String string) { for (int i = 0; i < string.length() - 1; i++) { if (string.substring(i + 1).contains(string.subSequence(i, i + 1))) { return true; } } return false; } }</source>[[Language::Java| ]]
Please note that all contributions to WikiJava are considered to be released under the GNU Free Documentation License 1.2 (see Project:Copyrights for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here. You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Summary:
This is a minor edit Watch this page
Cancel