【问题标题】:Exercise about reading sentences in JavaJava中阅读句子的练习
【发布时间】:2018-01-13 12:24:44
【问题描述】:

我正在尝试阅读 Java 中的一个句子并知道其中有多少个单词。这就是我所做的:

public class TestWords {
    public static void main(String[] args) {
        System.out.println("Give your phrase");
        Scanner extr=new Scanner(System.in);

        String Phrase;
        Phrase = extr.nextLine();
        int TotalSizeOfPhrase = Phrase.length();

        double number;
        for (int i=0; i < TotalSizeOfPhrase; i++)
        {
            if (Phrase[i] != number &&  Character.isWhitespace(s.charAt(i)))                    
            {
                TotalWords = TotalWords + 1;   
            }
        }
    }
}

我想知道如何编码:

if (Phrase[i]!= 'of an **arbitrary** number && white space')

然后:

TotalWords = TotalWords + 1;

因为当我输入这个时它标志着一个错误:

Character.isWhitespace(s.charAt(i))

【问题讨论】:

  • 使用Phrase.chatAt,尽管您可以在空间上拆分Phrase,只需根据数字检查值并增加您的tot
  • @Pavneet_Singh 我实际上输入的是 Phase.charAt 而不是 s,但错误仍然存​​在
  • @Pavneet_Singh 我编辑了我的问题
  • Phrase[i] != number 还需要与 charAt 一起使用,Phrase 是字符串而不是数组

标签: java string netbeans data-structures


【解决方案1】:

有几个错误

    System.out.println("Give your phrase : ");
    Scanner scan = new Scanner(System.in);
    String Phrase;
    Phrase = scan.nextLine();
    System.out.println("Enter age : ");
    int number = scan.nextInt();
    // replace the number with empty string mean nothing
    Phrase = Phrase.replace(String.valueOf(number), "");
    Phrase = Phrase.concat(" "); // add space at end for loop calculation 
    int TotalSizeOfPhrase = 0;   // set tot =0
    int count=0; // a count variable to keep track of the word length
    for (int i=0; i<Phrase.length(); i++)
    {
        count++;
        if(Character.isWhitespace(Phrase.charAt(i)))
        {
            if(count-1>1){ // if size of word ( -1 is there for space size) 
                           // is greater than 1 than increment count
                TotalSizeOfPhrase=TotalSizeOfPhrase+1;                          
            }
            count=0;            
        }
    }
    System.out.println(TotalSizeOfPhrase);
    scan.close();// don't forget

输入:

Hello i'm 20 and I'm a beginner
20

输出:

5

【讨论】:

  • 嗯嗯,我不应该在代码中分配一个数字,比如 9,我应该使用 Scanner 读取它(我可以这样做:),为什么输出是 6?它应该是 0,因为没有单词,只有数字
  • 所以你想计算特定数字的出现次数?
  • 在您的帖子中添加一些带有预期输入和输出的演示测试用例
  • 例如,如果我给出这句话:“你好,我 20 岁,我是初学者”,那么程序应该有输出 5,因为只有 5 个单词
  • 数字或字母不算作单词
【解决方案2】:

我这样做的方法是用空格分割行(获取单词),将它们添加到数组中,然后得到这个数组长度,该长度等于字数。

Phrase = Phrase.trim(); //make sure there is no spaces at start or end of the line
String[] words = Phrase.split(" "); //get the words 
int word_count = words.length;  //get the word count in line

【讨论】:

    【解决方案3】:

    如果你想得到句子中的单词数,你可以使用这个代码:

    int numberOfWords = Phrase.trim().isEmpty() ? 0 : trim.split("\\s+").length;
    

    【讨论】:

    【解决方案4】:

    您可以使用此代码:

    public static void main(String[] args) {
        System.out.println("Give your phrase");
        Scanner extr = new Scanner(System.in);
    
        String Phrase;
    
        Phrase = extr.nextLine();
    
        String[] words = Phrase.trim().split(" ");
    
        System.out.println("Totals Number Of Words: " + words.length);
    
        for (String word : words) {
            System.out.println(word.trim());
        }
    
    }
    

    【讨论】:

    • 23 是一个词吗?不,这是一个数字
    • 等我有一个更好的,输入你的代码 0 0 0 0 0 ,你会得到 5 个单词作为输出
    • 但我不得不说,代码看起来又好又简单:) 我理解得很好
    • 这与您定义“单词”的方式有关。实际上,根据定义,数字是一个词。如果您打开任何文字处理器程序或记事本++并键入 0 0 0 0 0 然后使用计数功能,您将得到 5。所以您的问题应该是“长度大于 1 且不包括数字和包括一个任意数字”
    • 输入如下: // 0 //,你会得到 3 个单词
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2010-11-06
    • 2012-10-11
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    相关资源
    最近更新 更多