0

此代码应允许用户输入一个句子,将其更改为小写,然后将每个单词的首字母大写。但我无法让扫描仪工作,它什么也没打印。有什么建议么?

public class Capitalize
{
    public static void capCase(String theString)
    {
        String source = theString;
        StringBuffer res = new StringBuffer();

        char[] chars = theString.toLowerCase().toCharArray();
        boolean found = false;
        for(int i = 0; i<chars.length; i++)
        {
            if(!found&& Character.isLetter(chars[i])){
                chars[i] = Character.toUpperCase(chars[i]);
                found = true;
            } else if (Character.isWhitespace(chars[i])){
                found = true;
            }
        }
    }

    public static void main(String[] args)
    {
        Scanner scanner=new Scanner(System.in);
        System.out.println(scanner.next());
    }
}
4

8 回答 8

1

我看到的问题:

  • 一旦用户按下回车,代码就只会打印输入的第一个单词
  • 该方法不返回任何内容,因此它有效地完成了所有工作并将其丢弃。

所以这是我可能做的:

为了简洁起见,我将把所有内容都放在 main 中

public class Capitalize {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String sentence = Scanner.nextLine();
        StringBuilder ans = new StringBuilder(); // result
        for(String s : sentence.split(" ")) { // splits the string at spaces and iterates through the words.
            char[] str = s.toLowerCase().toCharArray(); // same as in OPs code
            if(str.Length>0) // can happen if there are two spaces in a row I believe
                str[0]=Character.toUpperCase(str[0]); // make the first character uppercase
            ans.Append(str); // add modified word to the result buffer
            ans.Append(' '); // add a space
        }
        System.out.println(ans);
    }
}
于 2013-12-07T06:32:30.147 回答
0

试试这个对我有用的代码:

import java.util.Scanner;

public class Capitalize {
  /**
   * This code should allow the user to input a sentence, change it to lower
   * case, and then capitalize the first letter of each word. But I can't get
   * the scanner to work, it just prints nothing. Any suggestions?
   * 
   * @param theString
   */
  public static void capCase(String theString) {

    String source = theString.trim();
    StringBuffer res = new StringBuffer();
    String lower = theString.toLowerCase();
    String[] split = lower.split(" ");
    for (int i = 0; i < split.length; i++) {
      String temp = split[i].trim();

      if (temp.matches("^[a-zA-Z]+")) {
        split[i] = temp.substring(0, 1).toUpperCase()
            + temp.substring(1);
      }
      res.append(split[i] + " ");
    }
    System.out.println(res.toString());

  }

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    capCase(scanner.nextLine());
    // System.out.println(scanner.next());
  }
}
于 2014-09-30T13:54:33.663 回答
0

您忘记调用该capCase()方法,您的代码只要求从标准输入输入并直接打印出来

于 2013-12-07T06:22:39.237 回答
0

我已经测试过了。有用。

import java.util.Scanner;

import org.apache.commons.lang3.text.WordUtils;


public class Capitalize {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        while(s.hasNextLine()) {
            System.out.println(WordUtils.capitalize(s.nextLine()));
        }

    }

}
于 2013-12-07T06:23:16.260 回答
0

问题 :

1.您需要发送完整的行并将字符串发送到函数capCase()
2.您没有将 char 数组返回给调用者。

解决方案
1.使用以下语句读取完整的行

String str=scanner.nextLine();

2.将返回类型capCase()从void更改char[]为如下:

public static char[] capCase(String theString)

您应该从函数返回char[]变量字符,如下所示:capCase()

return chars;

完整代码:

public static char[] capCase(String theString)
{

String source = theString;
StringBuffer res = new StringBuffer();

char[] chars = theString.toLowerCase().toCharArray();
boolean found = false;
for(int i = 0; i<chars.length; i++)
{
    if(!found&& Character.isLetter(chars[i])){
        chars[i] = Character.toUpperCase(chars[i]);
        found = true;
    } else if (Character.isWhitespace(chars[i])){
        found = true;
    }
}

return chars;
}

public static void main(String[] args)
{
    Scanner scanner=new Scanner(System.in);
    String str=scanner.nextLine();
    
    System.out.println(capCase(str));
}
于 2013-12-07T06:24:48.143 回答
0

尝试,

public static void main(String[] args) {
    System.out.println(capCase("hello world"));
}

public static String capCase(String theString) {

    StringBuilder res = new StringBuilder();

    String[] words=theString.split(" +");
    for (String word : words) {
        char ch=Character.toUpperCase(word.charAt(0));
        word=ch+word.substring(1);
        res.append(word).append(" ");
    }

    return res.toString();
}
于 2013-12-07T06:37:31.427 回答
0

我尝试在 main 方法中运行程序,它对我来说运行良好。但是如果你想得到整个句子,你必须像迭代器一样调用scanner,然后通过调用scanner.next() 方法获取每个下一个标记,Scanner 根据空格分隔句子中的单词。我的示例实现如下。您可以在函数中传递每个单词来处理它。

`public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    while (scanner.hasNext())
        System.out.println(scanner.next());
}`
于 2013-12-07T06:26:42.370 回答
0

我可能会这样做

public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  while (scanner.hasNextLine()) { // While there is input.
    String line = scanner.nextLine(); // read a line.
    int i = 0;
    for (String s : line.split(" ")) { // split on space... word(s).
      if (i != 0) {
        System.out.print(" "); // add a space, except for the first word on a line.
      }
      System.out.print(capCase(s)); // capCase the word.
      i++; // increment our word count.
    }
    System.out.println(); // add a line.
    System.out.flush(); // flush!
  }
}

public static String capCase(String theString) {
  if (theString == null) {
    return ""; // Better safe.
  }
  StringBuilder sb = new StringBuilder(theString
      .trim().toLowerCase()); // lowercase the string.
  if (sb.length() > 0) {
    char c = sb.charAt(0);
    sb.setCharAt(0, Character.toUpperCase(c)); // uppercase the first character.
  }
  return sb.toString(); // return the word.
}
于 2013-12-07T06:29:37.177 回答