Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-02-2008, 01:58 AM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
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()

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"); }...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-02-2008, 06:07 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 260
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-02-2008, 03:37 PM
Member
 
Join Date: Nov 2007
Posts: 20
Gilgamesh is on a distinguished road
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 01:08 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-02-2008, 10:10 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 260
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying COPYRIGHT and REGISTERED characters Java Tip Java Tips 0 03-01-2008 10:52 PM
Displaying characters in many ways. TampaTechGuy New To Java 7 01-02-2008 10:16 PM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
special characters ravian New To Java 2 11-16-2007 02:28 PM
Getting all characters in a String Alayna New To Java 2 05-20-2007 12:49 PM


All times are GMT +3. The time now is 06:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org