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;
public class Test {
public static void main(String[] args) {
String str="Chocolate";
int n=2;
String x=frontTimes(str,n);
}
public static String frontTimes(String str, int n) {
if(str.length()==1){
String sub=str;
}
if(str.length()==2){
String sub=str.substring(0,2);
}
else{
String sub=str.substring(0,3);
}
String sub=str.substring(0,3);// This statement works. But without this, I get an error. Why does it not enter the else condition without this line?
String result="";
for(int i=0;i<n;i++){
result=result +sub;
}
return result;
}
}