Results 1 to 4 of 4
  1. #1
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default characters + strings

    i have a String and i want to test if there is at least one (different) character that is repeated three times (or more) consequently

    e.g. aaabcde->true
    abbbcde->true
    abbbcdddde->true

    i have to use 1) the charAt(), 2) substring(), 3) length()

    Java Code:
    ...String s=readLine("type a string.");
    		if (s.length()>=3){
    			int i=0;
    			for (i=0; i<s.length()-2;i++){ 			
    		/**i am stuck here	(how to say substring(i, i+2) equals to three times the charAt(i)	*/			                                     println("true");
    					break;
    				}
    				}
    												
    			}else{ println("false");
    		}...

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    		if (s.length() >= 3) {
    			for (int i = 0; i < s.length() - 2; i++) {
    
    				char c = s.charAt(i);
    				String desired = "" + c + c + c;
    				String actual = s.substring(i, i + 3);
    
    				if (desired.equals(actual)) {
    					// It matched ..
    					System.out.println("True");
    				}
    			}
    
    		}
    dont worry newbie, we got you covered.

  3. #3
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    by the way, i understand that the "" here: String desired=""+c+c+c; makes a String containing this ccc , but how?

    thank you
    Last edited by Gilgamesh; 03-03-2008 at 12:08 AM.

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    char + char is is taken as integer addition. This code,
    char d = 'a' ;
    System.out.println(d+d);

    Will print 194

    "" Will make all other following to be converted to String. Try with int as well.
    dont worry newbie, we got you covered.

Similar Threads

  1. Displaying COPYRIGHT and REGISTERED characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 09:52 PM
  2. Displaying characters in many ways.
    By TampaTechGuy in forum New To Java
    Replies: 7
    Last Post: 01-02-2008, 09:16 PM
  3. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 02:38 AM
  4. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:28 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •