简体   繁体   English

如何输入并让程序在新行上打印出每个字母,同时遇到每个空格以打印出“ <space> “?

[英]How do I take an input and make the program print out each letter on a new line while making every space encounter to print out “<space>”?

import java.util.Scanner;

public class TwoDotSevenNumbaTwo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String input;
        int num1, num2, leng;
        char word;
        Scanner inputscan = new Scanner(System.in);
        System.out.println("Give me some love baby");
        input = inputscan.nextLine();
        leng = input.length();
        num1 = 0;
        num2 = 1;

        while (num1 < leng) {
            input = input.replaceAll(" ", "<space>");
            System.out.println(input.charAt(num1));

            num1++;
        }
    }
}

I can't seem to figure out how to get the <space> on a single line. 我似乎无法弄清楚如何在单行上获取<space> I know I can't do it because it is a char but I can't find a way around it. 我知道我做不到,因为它是一个字符,但是我找不到解决方法。

You could do 你可以做

for(int i = 0; i < leng; ++i) {
   char x = input.charAt(i);
   System.out.println(x == ' ' ? "<space>" : x);
}

Once you've stored the input as a String, you can write: 将输入存储为字符串后,可以编写:

// Break the string into individual chars
for (char c: input.toCharArray()) {
    if (c == ' ') { // If the char is a space...
        System.out.println("<space>");
    }
    else { // Otherwise...
        System.out.println(c);
    }
}

Regex powa: 正则表达式powa:

yourText.replaceAll(".", "$0\n").replaceAll(" ","<space>");

Explanation: 说明:

First replaceAll takes every charachter ( . = any character) and replaces it with the same character ( $0 = matched text) followed by a newline \\n , thusly every character is on separate line. 首先replaceAll接受每个字符( . =任何字符),并用相同的字符( $0 =匹配的文本)替换,后跟换行符\\n ,因此每个字符都位于单独的行上。

Second replaceAll just replaces every acctual space with the word "<space>" 第二个replaceAll只是用单词"<space>"替换每个实际的空格

For java regex tutorial, you can follow this link or use your favourite search engine to find plenty more. 对于Java regex教程,您可以单击此链接或使用自己喜欢的搜索引擎查找更多内容。

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

相关问题 如何获得此程序以每行打印出十个项目,并以1个空格分隔? - How can I get this program to print out ten items per line seperated by 1 space? 需要程序找到空间并提取空间左侧和右侧的变量并打印出来。 我该怎么做? - Need the program to find space and extract variables to left and right of space and print them out. How do I do it? 获取用户输入,放入数组并打印出每个字母的使用次数 - Take user input, put into an array and print out how many times each letter is used 如何让我的程序从 do while 循环中打印出我的所有输入? - How do I get my program to print out all of my input from a do while loop? 如何更改代码以每次在新行中将其打印出来? - How can I change my code to print it out every time on new line? 输入并指定要打印的字符以及每行要打印的字符数 - input and specify the character to print out and the number of characters to print on each line 如何打印每行的第一个单词? - How would I print out the first word of each line? 如何修改此Java命令行程序以搜索字符串并打印出该字符串出现的次数? - How do I modify this java command line program to search for a string and print out the number occurrences of that string? 我该如何打印出来? - How do i print this out? 打印输入中的每个字符 - print out each character in an input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM