0

i'm trying to do a method like this

"String",number of the Character, number of repetisions

repeatAtFrontAndEnd("Hello", 2, 1) → "HeHelloHe"
repeatAtFrontAndEnd("Hello", 1, 3) → "HHHHelloHHH"
repeatAtFrontAndEnd("Hi There", 3, 2) → "Hi Hi Hi ThereHi Hi "

if anybody can help me :D

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by masijade
0

i got this lol

public String repeatAfFrontAndEnd(String str, int nChars, int nRepeated){
String temp= str;
if((nChars==0)||(nChars>str.length())){
System.out.print("a");
return str;
}
for(int i=0;i <=str.length()){
nRepeated = str.charAt(nChars);
//nChars = str.charAt(i);
System.out.print(nRepeated);
//for(int j=0;j>nRepeated;j++){
System.out.print("c");


nRepeated =nChars ;
temp=nChars + str + nChars;
System.out.print("d");
return temp;
}
return str;
}

Edited by happygeek: fixed formatting

0

the method "substring" of String, will give you the piece of the String to "repeat". Then you simply need to put it out "nRepeated" number of times (j < nRepeated not j > nRepeated) both before and after the String.

Read the API docs for String and it's substring method and give it another try.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.