Given a string and a nonnegative int n well say that the fro
Given a string and a non-negative int n, we\'ll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;
frontTimes(\"Chocolate\", 2) \"ChoCho\"
frontTimes(\"Chocolate\", 3) \"ChoChoCho\"
frontTimes(\"Abc\", 3) \"AbcAbcAbc\"
Must work the following problem using a while loop or do while.
Solution
StringTimesTest.java
public class StringTimesTest {
public static void main(String[] args) {
System.out.println(frontTimes(\"Chocolate\", 2) );
System.out.println(frontTimes(\"Chocolate\", 3) );
System.out.println(frontTimes(\"Abc\", 3) );
}
public static String stringTimes(String s, int n){
String returnString = \"\";
int i=0;
while(i<n){
returnString = returnString + s;
i++;
}
return returnString;
}
public static String frontTimes(String s, int n){
if(s.length()<=3){
return stringTimes(s, n);
}
else{
return stringTimes(s.substring(0,3), n);
}
}
}
Output:
ChoCho
ChoChoCho
AbcAbcAbc
