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

Recommended Answers

All 3 Replies

Well, what do you have and what errors are you getting?

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;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.