-1

I'm working on a class assignment with a few individual parts, which I have all done with the exception of this one. I need to get a string input from the user and create a loop (preferably a for loop) that inserts asterisks between each character. I'm completely stumped on this one so if someone could just give me some help to get started it would be appreciated.

I've come up with this so far

    } else if (choice.equalsIgnoreCase("C")) {
        System.out.print("Enter text here: ");
        String orig = input.nextLine();

        // To use for asterisk insertion
        int x = 1;
        int y = 2;

        for (int length = orig.length(); length > 0;) {
            orig = orig.substring(0,x) + "*" + orig.substring(y);
            x = x + 2;
            y = y + 2;
        }

    } 

It compiles just fine but when I test it and enter some text it comes up with Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5

user2827341
  • 3
  • 1
  • 4
  • Instead of using substring, it would be much easier, intuitive and efficient to simply recreate a new string by appending each char of the original string followed by an asterisk (except for the last one) to a StringBuilder. You can of course also append an asterisk before each character except the first one. – JB Nizet Sep 29 '13 at 21:49
  • The only problem with that might be that we haven't learned how to use StringBuilder yet so I'm not sure if I should use that method. Also the example output puts the first asterisk after the first character and the last asterisk after the last character. ie; h*i* – user2827341 Sep 29 '13 at 21:52
  • I think with the error that I'm receiving I just need to find a way to tell the loop when to stop doing this process as it is going to the end and then messing up? – user2827341 Sep 29 '13 at 21:53
  • StringBuilder is a class like all the other classes. It has public methods, and a public javadoc. If your teacher doesn't accept the fact that you can find a class and read its documentation by yourself, then all hope is lost. You can of course choose to use STring only, by creating an array of chars and the String constructor. – JB Nizet Sep 29 '13 at 21:56
  • I've been debating using StringBuilder the entire time as looking up how to insert characters into strings has led me to it many times. It does seem much easier to use, I think I'll go with it. Thanks for the advice. – user2827341 Sep 29 '13 at 22:14

3 Answers3

0

Don't reuse variables unless you have to. It only makes code more complicated and difficult to read.

    String orig = "my test";
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < orig.length() -1;i++) {
        result.append(orig.charAt(i));
        result.append('*');
    }
    result.append(orig.charAt(orig.length()-1));
BevynQ
  • 8,089
  • 4
  • 25
  • 37
0

Your for loop has no valid terminating clause.

for (int length = orig.length(); length > 0;)

will run indefinitely until it causes an Exception.

    System.out.print("Enter text here: ");
    String orig = input.nextLine();

    for (int length = orig.length() - 1; length > 0; length--) {
        orig = orig.substring(0,length) + "*" + orig.substring(length);
    }

Produces

Enter text here: TestOutput T*e*s*t*O*u*t*p*u*t

If you want an * after the final 't' than you can easily add that by removing the "- 1" in the for loop.

0

try this program to add asterisks between each character of the string.

int leng;
Scanner sc= new Scanner(System.in);
System.out.println("enter the name");
String name=sc.nextLine();
leng=name.length();
char c[]=new char[leng];
for(int i=0;i<=leng-1;i++){
    c[i]=name.charAt(i);

    System.out.print(c[i]);
    if(i<leng-1)
        System.out.print("*");
}
ashi
  • 1
  • 3