Results 1 to 7 of 7
Thread: While-loop problem
- 11-02-2009, 02:36 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
While-loop problem
Can some one tell me what I did wrong here?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" */
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; }
-
Why do you think you've done something wrong? Let's see the program where you test this method.
- 11-02-2009, 02:46 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
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%)
- 11-02-2009, 02:47 AM #4
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
- 11-02-2009, 02:57 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
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
Test resultJava 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; }
Java Code:Testing method repeatSeparator in student class WhileLoopProblems -------------------------------------------------------------------------------- X repeatSeparator("AAA", "", 0)-> expected:<[]> but was:<[AAA]> -------------------------------------------------------------------------------- 10/11 tests passing (91%)
-
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.
- 11-02-2009, 03:10 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Help with a loop-like problem
By Jnoobs in forum New To JavaReplies: 1Last Post: 10-14-2009, 01:15 AM -
if else loop problem
By Ms.Ranjan in forum New To JavaReplies: 12Last Post: 04-25-2009, 09:30 AM -
Loop Problem
By jralexander in forum New To JavaReplies: 4Last Post: 12-02-2008, 07:08 AM -
Problem to use different for loop to add up
By matt_well in forum New To JavaReplies: 6Last Post: 08-03-2008, 10:24 PM -
For loop problem
By mcal in forum New To JavaReplies: 32Last Post: 01-25-2008, 03:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks