3

I'm fairly new to programming. I'm trying to repeat the word in a given string the amount of times by a given number in the same string. I have decided to loop through the string and add each char to a new string to print out but I'm getting an out of index error.

final String string = "Hello";
final int num = 3;

int number = string.length() * num;
String str = "";

for (int i = 0; i < number; i++) {
    str += string.charAt(i);
}

System.out.println(str);
Rednuam
  • 47
  • 5
  • Thanks for the solve. Noted and I will take in for future code. – Rednuam Jul 30 '21 at 21:40
  • 1
    An example of input and output would improve this Question. – Basil Bourque Jul 31 '21 at 22:52
  • 1
    By the way, the `char` type in Java is obsolete. Unable to handle even half the characters defined in Unicode. Learn to use Unicode [code point](https://en.wikipedia.org/wiki/Code_point) integer numbers instead. – Basil Bourque Jul 31 '21 at 22:54

3 Answers3

3

Zero-based index

You are getting the error because the value of i is going beyond the last index available in Hello. The last index in Hello is "Hello".length() - 1 whereas the value of i is going beyond this value because of your loop terminating condition:

i < string.length() *  num;

By the way, if you want to repeat Hello 3 times, you should do it as

for(int i = 0; i < num; i ++){
   System.out.print(string);
}

Demo:

public class Main {
    public static void main(String[] args) {
        final String string = "Hello";
        final int num = 3;
        for (int i = 0; i < num; i++) {
            System.out.print(string);
        }
    }
}

String#repeat

With Java 11+, you can do it without using a loop by using String#repeat:

System.out.println(string.repeat(num));

Demo:

public class Main {
    public static void main(String[] args) {
        final String string = "Hello";
        final int num = 3;
        System.out.println(string.repeat(num));
    }
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 2
    Thanks for explaining why i was getting out of index error. That really helped me to solve my issue. – Rednuam Jul 30 '21 at 21:37
2

Just keep it simple

String string = "Hello";
int num = 3;
for (int i = 0; i < num; i++) {
    System.out.println(string);
}

If you want to have your result in a new String you can just do this :

String string = "Hello";
int num = 3;
String res = "";
for (int i = 0; i < num; i++) {
    res += string;
}
System.out.println(res);
T.K
  • 434
  • 7
  • 14
  • I didn't realise I could just add the original string to the new one instead of trying to add each char that I looped through. Thanks! – Rednuam Jul 30 '21 at 21:39
1

Just adding this in here since you stated you're learning. Java has a StringBuilder class, super easy to use

And according this this induvial class using StringBuilder is incredibly more efficient than concatenate.

StringBuilder vs String concatenation in toString() in Java