0

Write a method called multiConcat that takes a String and an integer as parameters. Return a String made up of the string parameter concatenated with itself count time, where count is the integer. for example, if the parameters values are “ hi” and 4, the return value is “hihihihi” Return the original string if the integer parameter is less than 2.

What i have so Far

import java.util.Scanner;
public class Methods_4_16 {
public static String multiConcat(int Print, String Text){
    String Msg;
    for(int i = 0; i < Print; i ++ ){

 }
    return(Msg);
 }

 public static void main(String[] args) {
    Scanner Input = new Scanner(System.in);
    int Prints;
    String Texts;

    System.out.print("Enter Text:");
    Texts = Input.nextLine();

    System.out.print("Enter amount you wanted printed:");
    Prints = Input.nextInt();

    System.out.print(multiConcat(Prints,Texts));



 }
}
Latusken
  • 17
  • 4

2 Answers2

1

Just a few hints:

  • concating strings can be done this way: appendTo += stuffToConcat
  • repeating an operation n times can be done with a for-loop of this kind:

    for(int i = 0 ; i < n ; i++){
        //do the stuff you want to repeat here
    }
    

Should be pretty simple to build the solution from these two parts. And just in case you get a NullPointerException: remember to initialize Msg.

0

Try this:

public static String multiConcat(int print, String text){
    StringBuilder msg = new StringBuilder();
    for(int i = 0; i < print; i ++ ) {
        msg.append(text);
    }
    return msg.toString();
}

I have used StringBuilder instead of a String. To know the difference, give this a read: String and StringBuilder.

Also, I would guess you are new to Java programming. Give this link a read. It is about Java naming conventions.

Hope this helps!