Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Count Singletons in a String

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Count Singletons in a String

    For a problem, I have to write a code that counts the number of singletons, stored in the variable n, in a string sequence. A singleton is a word in a sequence that does not appear before or after itself in the sequence.

    I initialized the count variable n with 2, since the first and last tokens can only have one
    copy before and after each other respectively. I divided the string sequence into tokens by splitting it with respect to the empty spaces between them.

    Here is my code.

    String string = " ";
    string = stdin.next();
     
    String[] tokens = string.split(" ");
     
    n = 2;
     
    for (int i = 1; i < tokens.length()-1; i++){
    	while (!(tokens[i].equals("xxxxx"))){ 
    		if (!(tokens[i].equals(tokens[i-1]) && !(tokens[i].equals(tokens[i+1])))
    			n++;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Count Singletons in a String

    Do you have a question?

    Also posted at: http://forums.devshed.com/java-help-...ng-960893.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Count Singletons in a String

    Quote Originally Posted by Norm View Post
    Do you have a question?

    Also posted at: Singletons in a string
    Yes, I know, I posted the same question on two different forums. I hope that this is not against the rules of the forums.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Count Singletons in a String

    Did you have any specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Count Singletons in a String

    What's your definition of 'singleton?' That term has a specific meaning in programming, and I don't think that's what you mean.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Count Singletons in a String

    Quote Originally Posted by GregBrannon View Post
    What's your definition of 'singleton?' That term has a specific meaning in programming, and I don't think that's what you mean.
    Well, the problem specification states that: "A singleton is a word in a sequence that does not appear before or after itself in the sequence."

    --- Update ---

    Quote Originally Posted by Norm View Post
    Did you have any specific questions?
    Yes, I would like to know if someone can help me figure out why my code is not working. It is suppose to find singletons in a string, and count the number of time the singletons appear.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Count Singletons in a String

    See the other site where this is posted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Count occurrence of a string in an array list
    By Parth Raval in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2014, 12:04 AM
  2. Using a Loop and Nested Ifs to Count Number of Vowels in a String
    By Potat in forum Loops & Control Statements
    Replies: 17
    Last Post: February 21st, 2013, 09:35 PM
  3. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  4. Should GUI Panels be Singletons?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 11
    Last Post: September 26th, 2012, 01:40 PM
  5. Finding the count of a letter in a String?
    By vlkn448 in forum Algorithms & Recursion
    Replies: 7
    Last Post: July 15th, 2012, 11:55 AM