简体   繁体   English

字符类-将字符串中每个单词的首字母大写

[英]Character Class- Capitalize the first letter of each word in a string

I'm having trouble with a program, in which i'm supposed to accept a string input, change it to char data and then capitalize the first letter of every word in the "string" using the Character class. 我在程序中遇到麻烦,在该程序中,我应该接受字符串输入,将其更改为char数据,然后使用Character类将每个字符串中的每个单词的首字母大写。

The code is the following: 代码如下:

import java.util.*;
public class wrapper
{
    public static void main(String[] args)
    {
        Scanner input= new Scanner(System.in);
        String s1;
        s1=input.nextLine();
        s1= s1.trim();
        int howLong= s1.length();
        int i;
        int counter;
        char ch;
        for(counter=0; counter<= howLong; counter++)
        {
            ch=s1.charAt(counter);            
            System.out.print(ch);
        }

        }
}

I'm just trying to change the string data to char data using a for loop at the moment, but the program won't run even though it compiles. 我现在只是尝试使用for循环将字符串数据更改为char数据,但是该程序即使编译也不会运行。 (I'm using the BlueJ IDE) (我正在使用BlueJ IDE)

You have to print a statement on the screen to enter a string, or, the console wouldn't open automatically. 您必须在屏幕上打印一条语句以输入字符串,否则控制台将不会自动打开。

Copy the program below and enter it instead of your program. 复制下面的程序,然后输入它而不是您的程序。 It'll work perfectly. 它会完美地工作。 The program is: 该程序是:

import java.util.*;
public class wrapper
{
    public static void main(String[] args)
    {
        System.out.println("Enter a string");
        Scanner input= new Scanner(System.in);
        String s1;
        s1=input.nextLine();
        s1= s1.trim();
        int howLong= s1.length();
        int i;
        int counter;
        char ch;
        for(counter=0; counter< howLong; counter++)
        {
            ch=s1.charAt(counter);            
            System.out.print(ch);
        }

        }
}

Hope this helped you! 希望这对您有所帮助!

Regards, 问候,

Rachit Bhargava 拉希特·巴尔加瓦(Rachit Bhargava)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM