Results 1 to 7 of 7
  1. #1
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default While-loop problem

    Java Code:
        /**
         *  Write the method repeatSeparator().
         *  
         *  @param word the first String parameter.
         *  @param sep the second String parameter.
         *  @param count the number of times to repeat the word.
         *  @return a new String as described below.  
         * 
         *  Given two strings, word and a separator, return 
         *  a big string made of count occurrences of the word, 
         *  separated by the separator string.
         *  
         *  Here are some examples:
         *      repeatSeparator("Word", "X", 3) returns "WordXWordXWord"
         *      repeatSeparator("This", "And", 2) returns "ThisAndThis"
         *      repeatSeparator("This", "And", 1) returns "This"
         */
    Can some one tell me what I did wrong here?

    Java Code:
        public String repeatSeparator(String word, String sep, int count) 
        {
        	String result = "";
        	
        	int i = 0;
        	while (i < count)
        	{
        	if( i < count - 1)
        	result += word + sep;
        	else
        	result += word;
        	
        	i++;
        	}
        	
        	return result;
        	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    I'm using check result that my teacher has given out to every homework assignment to test if my code run correctly.

    When I try to check if i did it correctly.

    This is what I got. I did use the while loop method, I just don't know why it doesn't read.

    Java Code:
    Testing method repeatSeparator in student class WhileLoopProblems
    --------------------------------------------------------------------------------
      X This method should use while loops exclusively.
    --------------------------------------------------------------------------------
       No tests run: (0%)

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    It works for me. The implementation isn't that readable, but that's a different question.

    I would comment that using a String (instead of a StringBuilder) is inefficient.


    Is the problem that you were expecting a static method (it's declared non-static)?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    It works for me. The implementation isn't that readable, but that's a different question.

    I would comment that using a String is inefficient.


    Is the problem that you were expecting a static method (it's declared non-static)?
    well, my teacher want me to return a different String.

    Okay i just play around with the codes and it read somehow, but i still miss one

    here is the codes
    Java Code:
       public String repeatSeparator(String word, String sep, int count) 
        {
        	String result = "";
        	
    
            int i = 0;
            while (i < count) 
            {
            if ( i < count - 1)	
              result += word + sep;
              i++;
            }
           
            result += word;
            
        	
        	return result;
        	
    }
    Test result

    Java Code:
    Testing method repeatSeparator in student class WhileLoopProblems
    --------------------------------------------------------------------------------
      X repeatSeparator("AAA", "", 0)-> expected:<[]> but was:<[AAA]>
    --------------------------------------------------------------------------------
       10/11 tests passing (91%)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jimmy-lin View Post
    well, my teacher want me to return a different String.
    If you used a StringBuilder, your method would return a String, it would return the results returned from the StringBuilder object's toString method. But I think that with your simple method, I wouldn't worry about using StringBuilder. Just concatenate Strings as you are doing as you really won't see much improvement in your program unless the method were concatenating hundreds of Strings or more.

  7. #7
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    ahh I figure it out, problem solve, thanks again for all your help.

Similar Threads

  1. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 02:15 AM
  2. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 10:30 AM
  3. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 08:08 AM
  4. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 11:24 PM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 04:51 PM

Posting Permissions

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