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);
3
  • Thanks for the solve. Noted and I will take in for future code. Commented Jul 30, 2021 at 21:40
  • 1
    An example of input and output would improve this Question. Commented Jul 31, 2021 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 integer numbers instead. Commented Jul 31, 2021 at 22:54

3 Answers 3

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));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining why i was getting out of index error. That really helped me to solve my issue.
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);

1 Comment

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!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.