You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取用户输入字符串并按格式输出单个字符?附Java代码

Solution for Formatting String Characters with Commas and Multi-Space Word Separators

Got it, let's fix up your code to meet the requirement: splitting each character with commas, and using multiple spaces (or   as shown in your example) between words. Here's the complete, working implementation with explanations:

Complete Java Code

import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any String:");
        String name = sc.nextLine();
        System.out.println("Before Splited: " + name);
        
        // Use StringBuilder for efficient string concatenation
        StringBuilder formattedResult = new StringBuilder();
        
        for (int i = 0; i < name.length(); i++) {
            char currentChar = name.charAt(i);
            
            if (currentChar == ' ') {
                // Add two non-breaking spaces (matches your example's &nbsp;&nbsp;)
                formattedResult.append("&nbsp;&nbsp;");
            } else {
                // Convert to lowercase to match your example's output (remove if not needed)
                formattedResult.append(Character.toLowerCase(currentChar));
                
                // Only add comma if it's not the last character, and next char isn't a space
                if (i != name.length() - 1 && name.charAt(i + 1) != ' ') {
                    formattedResult.append(",");
                }
            }
        }
        
        System.out.println("After Formatted: " + formattedResult);
        sc.close();
    }
}

Key Logic Breakdown

  • Efficient String Building: We use StringBuilder instead of repeated String concatenation because it's faster and uses less memory, especially for longer strings.
  • Word Separation Handling: When we hit a space (word boundary), we append two &nbsp; (if you need console output instead of HTML, replace this with two regular spaces ).
  • Character & Comma Placement:
    • Each non-space character is converted to lowercase (to match your example's j,a,v,a instead of J,a,v,a — remove Character.toLowerCase() if you want to keep original case).
    • We only add a comma if:
      1. It's not the very last character in the string.
      2. The next character isn't a space (so we don't add a comma right before a word separator).

Example Test Run

If you input:

Java is one of the most popular language

The output will be:

j,a,v,a   i,s   o,n,e   o,f   t,h,e   m,o,s,t   p,o,p,u,l,a,r   l,a,n,g,u,a,g,e

Quick Adjustments

  • Keep original character case: Remove Character.toLowerCase() from the code.
  • Console-friendly spacing: Replace "&nbsp;&nbsp;" with " " (two regular spaces).

内容的提问来源于stack exchange,提问作者Amit Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:36:47