-1

I'm trying to figure out how to print any inputted string multiple times in java. This is what I have thus far...

public class Main{

    public static String printGivenStringTimesNumberGiven(String str, int n) {
        for (int i=0; i<n; i++)
            str += n;
        return str;
    }

    //Don't touch here
    public static void main(String[] args){
        System.out.println(printGivenStringTimesNumberGiven("Ha", 3));
    }
}

I keep on getting Ha333 multiple times and I'm not sure where to go from here. Any help would be appreciated. I'm trying to get it to print out HaHaHa.

boldercoder
  • 49
  • 1
  • 8
  • Welcome to Java world :) Basically, you created a method which has parameters as a String and the given number, in this case, it is 3. You can start looking at how to debug at http://www.vogella.com/tutorials/EclipseDebugging/article.html to understand more about what you have done so far. – Tuan Hoang Nov 25 '17 at 04:26

1 Answers1

0

This should work in your case:

public class Main{
public static String printGivenStringTimesNumberGiven(String str, int n) {
   String s = str;
   for(int i = 0; i< n-1;i++)
       s = s +" " + " " + str;
return s;
}

//Don't touch here
public static void main(String[] args){
    System.out.println(printGivenStringTimesNumberGiven("Ha", 3));
}
}
Luai Ghunim
  • 976
  • 7
  • 14