0

Basically I'm trying to make a code in Java where I am inputting 5 letters, for example "Hello". The code needs to spit an output with each of the letters spaced apart by ten spacing. I also have to use printf and space them out using the %. I was able to do this without using %, by using .replace, however that's not what I'm supposed to be doing. So the output should be: H e l l o

Scanner scanner = new Scanner(System.in);
System.out.println("Enter five chatracters: ");
String charoutput = scanner.next();
String charoutput2 = charoutput.replace("", "       ");

System.out.println("You have entered: " +charoutput2);
Forrest
  • 141
  • 12
  • check out the answers to https://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java to help with the space padding. as for the 5 letters i'd use a loop – RAZ_Muh_Taz Sep 12 '18 at 17:27

1 Answers1

1
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter five chatracters: ");

    // You can include validation of the length of the input line if necessary
    String answer = scanner.next();
    String[] charArr = answer.substring(0, 5).split("");
    String result = Arrays.stream(charArr)
            .map(p -> String.format("%s%10s", p, ""))
            .collect(Collectors.joining());

    System.out.println(result);
    scanner.close();
Valentyn Hruzytskyi
  • 1,125
  • 1
  • 13
  • 31