3 Replies Latest reply: Jan 30, 2008 11:48 PM by 807603 RSS

    concatinating numbers with a string...sort of

    807603
      Well I've got an assignment that says this "Write a method called multiConcat that takes a string and an interger as parameters. Return a string that consists of the string parameter concatenated with itself count time, where count is the integer parameter. For example, if the parameter vales are 'hi" and 4, the return value is "hihihihi". Return the original string if the interger parameter is less than two.

      So I've tried a couple of things that I thought would work with my small amount of CS courses, but I have not been able to come up with a resolution for this assignment yet. I do not want anyone to post any time of code yet. All I ask for is for someone to point me in the correct direction with some kind of a learning document etc.
        • 1. Re: concatinating numbers with a string...sort of
          807603
          walk through the java.lang.String or java.lang.StringBuffer.
          • 2. Re: concatinating numbers with a string...sort of
            807603
            for that the answer is

            class MyClass{

                 StringBuffer string1=null;

            public      StringBuffer myMethod(String string , int number){

            string1=new StringBuffer(string);

                      if (number<2){
                           return string1;
                      }else{
                           while(number!=1){
                                string1.append(string);//here string is String and string1 is StringBuffer tehn only we can get coorect data.then u will get output hihihihi for input hi,4
                                //try it by keeping both of type stringBuffer, u will get out put hihihihihihihihi for input hi,4

                                --number;
                 }
                           return string1;
                      }
            }
            }

            class FirstProgram {

                 public static void main(String[] args) {

                      String string=args[0];
                      int number=Integer.parseInt(args[1]);
                      System.out.println("string ="+string+" "+"number ="+number);

                      StringBuffer result=(new MyClass()).myMethod(string,number);
                      System.out.println("final output is"+result);
            }
            }
            • 3. Re: concatinating numbers with a string...sort of
              807603
              Why are you making his complete homework instead of giving technical suggestions which API's to use?