【问题标题】:How do I get my output to contain the spaces I need?如何让我的输出包含我需要的空间?
【发布时间】:2019-10-02 21:49:20
【问题描述】:

我必须将传入字符串的每个单词的第一个字母大写。我的输出是大写,但它没有保持原始输出的格式。例如,字符串输入是“hello world”,我的输出是“HelloWorld”,我想要的输出应该是“Hello World”。

我尝试在整个代码中添加空格,但没有任何效果。我认为问题是当我使用 toCharArray 时,它给了我一个没有空格的输出?所以我的连接结果是一口气添加所有内容,而不是单独添加每个单词?

或者我认为我的代码在结果中使用了字符串连接,并且它没有被分开,因为我将两个单词都放入同一个变量中。

import java.util.*; 
import java.io.*;

    class Main {  
      public static String LetterCapitalize(String str) { 

        // code goes here   
        String[] word = str.split(" ");
        String result = "";


        for(int i = 0; i < word.length; i++) {

            char[] charWord = word[i].toCharArray();

            for(int j = 0; j < charWord.length; j++ ) {
            String cap = word[i].charAt(0) + "";
            cap = cap.toUpperCase();
            //System.out.print(" ");
            result += (j == 0 ? cap : word[i].charAt(j));
        }

        }

        return result;


      } 

      public static void main (String[] args) {  
        // keep this function call here     
        Scanner s = new Scanner(System.in);
        System.out.print(LetterCapitalize(s.nextLine())); 
      }   

   }

没有错误。只是没有得到想要的输出。

【问题讨论】:

  • 您的代码没有在结果中添加任何空格。

标签: java loops char spacing


【解决方案1】:

当您执行String[] word = str.split(" "); 时,每个单词之间的空格被取出,您现在只剩下数组中的单词。您应该在结果 words 数组上使用 String.join(" ", word) 来反转效果,以便恢复空格。

不要逐个字符地遍历每个单词,试试这个:

for(int i = 0; i < word.length; i++) {
    word[i] = word[i].substring(0, 1).toUpperCase() + word[i].substring(1);
}
result = String.join(" ", word);

【讨论】:

    【解决方案2】:

    您可以使用以下代码。

    class Main {
    public static String LetterCapitalize(String str) {
    
        // code goes here
        String[] word = str.split(" ");
        StringBuilder result = new StringBuilder();
    
    
        for (int i = 0; i < word.length; i++) {
    
            char[] charWord = word[i].toCharArray();
    
            for (int j = 0; j < charWord.length; j++) {
                String cap = word[i].charAt(0) + "";
                cap = cap.toUpperCase();
                //System.out.print(" ");
                result.append(j == 0 ? cap : word[i].charAt(j));
            }
            result.append(" ");
        }
    
        return result.toString();
    
    
    }
    
    public static void main(String[] args) {
        // keep this function call here
        Scanner s = new Scanner(System.in);
        System.out.print(LetterCapitalize(s.nextLine()));
    }
    

    }

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。您可以使用edit 按钮改进此答案以获得更多选票和声誉!
    【解决方案3】:

    试试这个:

    import java.util.*;
    
    class Main {  
      public static String LetterCapitalize(String str) { 
    
        // code goes here   
        String[] word = str.split(" ");
        String result = "";
    
        for(int i = 0; i < word.length; i++) {
            result += capitalize(word[i]) + (i != word.length - 1 ? " " : "");
        }
    
        return result;
      }
    
      private static String capitalize(String s){
          return Character.toUpperCase(s.charAt(0)) + s.substring(1);
      }
    
      public static void main (String[] args) {  
        // keep this function call here     
        Scanner s = new Scanner(System.in);
        System.out.print(LetterCapitalize(s.nextLine())); 
      }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      相关资源
      最近更新 更多