2

I want a user to enter a String and then add a space in between a character at a chosen interval.

example: user enters: hello then asks for a space every 2 letters. output = he_ll_o_

import java.util.Scanner;

public class stackOverflow {


    public static void main(String[] args) {


        System.out.println("enter a string");
        Scanner input = new Scanner(System.in);
        String getInput = input.nextLine();

        System.out.println("how many spaces would you like?");
        Scanner space = new Scanner(System.in);
        int getSpace = space.nextInt();


        String toInput2 = new String();

        for(int i = 0; getInput.length() > i; i++){

        if(getSpace == 0) {
            toInput2 = toInput2 + getInput.charAt(i);
            } 
        else if(i % getSpace == 0) {
            toInput2 = toInput2 + getInput.charAt(i) + "_"; //this line im having trouble with.
            }

        }


        System.out.println(toInput2);

    }



}

Thats my code so far, it might be the completely wrong way of solving it, so correct me if im wrong. thanks in advance :)

Jens
  • 67,715
  • 15
  • 98
  • 113
BriannaXD
  • 169
  • 2
  • 14
  • It's not a great idea to name your String `getInput`, as the prefix `get` is per convention reserved for getter and setter methods. See http://stackoverflow.com/questions/1568091/why-use-getters-and-setters In general it's unusual to use verbs for variable names... – Robert Mar 31 '15 at 08:33
  • and either your example or your description is wrong, because you add a space after the `o` of `hello` ... – Robert Mar 31 '15 at 08:39
  • well if there is no underscore and just whitespace, which is what im doing it wouldnt matter if there was a space after the o. And this is just a example, couldnt care less what my variable names are. @Robert ty though :) – BriannaXD Mar 31 '15 at 09:05

2 Answers2

4

I think you would want to formulate your loop body as follows:

for(int i = 0; getInput.length() > i; i++) {
    if (i != 0 && i % getSpace == 0)
        toInput2 = toInput2 + "_";

    toInput2 = toInput2 + getInput.charAt(i);
}

But, there's a simpler way, using regular expressions:

"helloworld".replaceAll(".{3}", "$0_")  // "hel_low_orl_d"
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • thanks, this helped a lot! Also just wondering, but at this line "toInput2 = toInput2 + getInput.charAt(i);" wouldnt that add a letter only when i != 0 && i % getSpace == 0? So when it says charAt(i) it would only add the letter at charAt(i), rather than every letter in the string? I know you're right and im wrong, im just curious. Thanks heaps again – BriannaXD Mar 31 '15 at 09:07
  • oh dont worry, just realised this is for(int i = 0; getInput.length() > i; i++) { if (i != 0 && i % getSpace == 0) toInput2 = toInput2 + "_"; toInput2 = toInput2 + getInput.charAt(i); } is the same as for(int i = 0; getInput.length() > i; i++) { if (i != 0 && i % getSpace == 0){ toInput2 = toInput2 + "_"; } toInput2 = toInput2 + getInput.charAt(i); } lol, sorry i waste your time – BriannaXD Mar 31 '15 at 09:20
0

You can simplify your case distinction to:

toInput2 += getInput.charAt(i);
if(i != 0 && i % getSpace == 0) {
    toInput2 += "_";
}

You should also think about renaming your variables.

aioobe
  • 413,195
  • 112
  • 811
  • 826
Sebastian P.
  • 818
  • 8
  • 20