简体   繁体   English

如何在一行上准确输入五个字符串,然后将每个字符串之间留有空格的字符串放出来? 爪哇

[英]How can I input exactly a five character string on a single line and then out put the string with spaces between each character? Java

I want a user to input five letters on a single line (for example the word "hello") and have the output be "hello". 我希望用户在一行上输入五个字母(例如单词“ hello”),输出为“ hello”。 I am able to do this with String.replace (as seen below) but I need to use printf and %s to do it . 我可以使用String.replace做到这一点(如下所示), 但是我需要使用printf%s来做到这一点

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);

You can use printf to print space for each character in string 您可以使用printf为字符串中的每个字符打印空间

String str = "hello";
    char[] ch =str.toCharArray();
    for (char c:ch) {
        System.out.printf("%2s", c);  // h e l l o
    }

or you can use Java 8 streams to do it in line 或者您可以使用Java 8流来实现

Arrays.stream(str.split("")).forEach(i->System.out.printf("%2s",i)); //h e l l o

There are multiple ways to solve this: 有多种解决方法:

Using String.join : 使用String.join

String input = "Hello";
String result = String.join(" ", input.split(""));
// result now holds "H e l l o"

Using regex with replaceAll: 将regex与replaceAll一起使用:

String input = "Hello";
String result = input.replaceAll(".","$0 ").trim();
// result now holds "H e l l o"

Using a loop: 使用循环:

String input = "Hello";
int length = input.length;
for(int i=0; i<length; i++)
  System.out.printf("%c%s", input.charAt(i), i<length ? " " : "");
// outputs "H e l l o"

Using printf : 使用printf

String input = "Hello";
for(char c : input.toCharArray())
  System.out.printf("%2c", c);
// outputs " H e l l o" (NOTE the leading space!)

Try it online. 在线尝试。

My preference would be the String.join , since it's basically a builtin of what you want to do. 我的首选是String.join ,因为它基本上是您要执行的内置操作。

Here is one of the ways you can do that: 这是您可以执行此操作的方法之一:

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

String separatedChars = "";
for(char c: charOutput.toCharArray()) {
    separatedChars += c + " ";
} 

System.out.printf("You have entered: %s", separatedChars);

Here you iterate through each char of the received string and add it to the separatedChars variable followed by a space. 在这里,您遍历接收到的字符串的每个字符,并将其添加到separatedChars变量后跟一个空格。 Then, the result is printed using printf . 然后,使用printf打印结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何拔出字符串的每个字符,并以相反的顺序放置? - How to pull out each character of string, and put in reverse order? Java:乘法表字符串需要修改以在每个字符之间获得空格 - Java: Multiplication table string needs to be modified to get spaces in between each character Java中的字符和字符串输入 - Character and String input in Java 当我尝试将“\\”字符放入 Java 字符串时,为什么会得到“以字符串文字结尾的非法行”? - Why I obtain an "Illegal line end in string literal" when I try to put the "\" character into a Java String? 我怎样才能打印出整个字符串而不仅仅是单个字符? - How can I get the whole String printed out and not only the single character? 如何获得程序以将每个字符打印为“字符1 :(字符),字符2 :(字符)等”? - How can I get my program to print out each character as “Character #1: (character) , Character #2: (character), etc”? 如何删除字符串中的单个字符(java)? - How to remove single character in a string (java)? 如何替换 Java 字符串中单个出现的字符 - How to replace single occurrence of a character in a Java string 如何使用输入将一个字符串中的5个字符准确地放置并隔开 - How do I use an input to out put exactly 5 characters in a string and space them out 如何为字符串Java中的每个字符赋值 - How to assign value to each character in string Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM