2012-08-02 53 views
-3

这是我使用java的字数统计程序。我需要重新编程这个东西,一些东西;什么?什么!有些东西算作一个字。这意味着它不应该计算同一个词两次,不管案件和标点符号如何。字数不重复

import java.util.Scanner; 
public class WordCount1 
{ 
    public static void main(String[]args) 
    { 
     final int Lines=6; 
     Scanner in=new Scanner (System.in); 
     String paragraph = ""; 
     System.out.println("Please input "+ Lines + " lines of text."); 
     for (int i=0; i < Lines; i+=1) 
     { 
      paragraph=paragraph+" "+in.nextLine(); 
     } 
     System.out.println(paragraph); 
     String word=""; 
     int WordCount=0; 
     for (int i=0; i<paragraph.length()-1; i+=1) 
     { 
      if (paragraph.charAt(i) != ' ' || paragraph.charAt(i) !=',' || paragraph.charAt(i) !=';' || paragraph.charAt(i) !=':') 
      { 
       word= word + paragraph.charAt(i); 
       if(paragraph.charAt(i+1)==' ' || paragraph.charAt(i) ==','|| paragraph.charAt(i) ==';' || paragraph.charAt(i) ==':') 
       { 
        WordCount +=1; 
        word=""; 
       } 
      } 
     } 
     System.out.println("There are "+WordCount +" words "); 
    } 
} 
+0

如果我这样做,我会在字串中搜索标点符号并将其删除。 – 2012-08-02 00:53:16

+5

1)格式化您的代码。 2)问一个实际的问题。 3)我们不会为你做你的功课... – 2012-08-02 00:54:13

+0

我明白,这是一个真正的问题。我也不会期待你。如果看起来这样,我很抱歉。 – 2012-08-02 01:04:10

回答

1

在进一步处理之前,您应该删除标点并更改为单个案例。 (注意区域设置和unicode)

将输入分解为单词后,可以通过将唯一单词传入Set并检查该设置的大小来计算唯一单词的数量。

+0

这不处理标点或不区分大小写。 – 2012-08-02 00:56:01

+0

你能写出完整的代码吗?我真的需要这个。 – 2012-08-02 00:56:45

+2

没有人会给你强制性的代码作业的问题。对不起,我错过了标点符号。这个问题很难理解。 – Antimony 2012-08-02 00:59:12

0

如果您知道要忽略的标记(;,?,!),则可以执行一个简单的String.replace以删除单词中的字符。您可能需要使用String.startsWithString.endsWith帮助

转换,则值更容易匹配(String.toLowercase

使用的“设置”是一个极好的主意小写。如果你想知道一个特定的词出现了多少次,你也可以采取某种

0
  1. 删除所有标点符号
  2. 转换所有字符串到大写或小写的Map的优势
  3. 把这些串在设置
  4. 获得集的大小
0
  1. 你需要剥离出标点符号;这里有一种方法:Translating strings character by character

  2. 上面也可以用来规范情况,虽然有可能是其他的实用程序。

  3. 现在,您所描述的所有变体都将被转换为相同的字符串,并因此被识别。正如其他人所建议的那样,按照设置来计算不同单词的数量是一个很好的工具。

3

既然这是功课,这里有一些提示和建议。

  • 有一个称为String.split一个聪明的小方法一个分割字符串转换成部分,使用指定为正则表达式的隔板。如果您以正确的方式使用它,这将为您提供“单词计数”问题的单行解决方案。 (如果您被告知不要使用拆分,您可以忽略......尽管这是一个经验丰富的Java开发人员首先会考虑的简单解决方案。)

  • 正确格式/缩进您的代码...之前你展示给其他人。如果你的教师没有为此扣分,他/她没有正确地完成他的工作。

  • 使用标准的Java命名约定。 Lines的大小写不正确。清单常量可能为LINES或变量为lines,但以大写字母开头的混合大小写名称始终为为类名称。

  • 在使用运算符周围的空白字符(包括赋值运算符)时应保持一致。

  • 硬连线用户必须提供的输入线数是一个坏主意(并且完全没有必要)。而且你没有处理他提供少于6行的情况。

+0

+1为正则表达式分裂,好主意 – MadProgrammer 2012-08-02 01:04:05

0

你真正的问题是,你想要有一个独特的wordcount,所以,你应该跟踪哪些单词已经遇到,或者完全从文本中删除它们。

假设您选择第一个,并将已经遇到的单词存储在列表中,那么您可以根据该列表检查您是否已经看到该单词。

List<String> encounteredWords = new ArrayList<String>(); 
// continue after that you found out what the word was 
if(!encounteredWords.contains(word.toLowerCase()){ 
    encounteredWords.add(word.toLowerCase()); 
    wordCount++; 
} 

但是,锑也做了一个有趣的评论,他使用Set的属性来看看不同的wordcount是什么。它被定义为一个集合永远不能包含重复,所以如果你只是添加更多的相同的单词,该集合将不会增长。

Set<String> wordSet = new HashSet<String>(); 
// continue after that you found out what the word was 
wordSet.add(word.toLowerCase()); 
// continue after that you scanned trough all words 
return wordSet.size(); 
1

Here you go。这工作。只是阅读评论,你应该能够遵循。

import java.util.Arrays; 
import java.util.HashSet; 
import javax.swing.JOptionPane; 

// Program Counts Words In A Sentence. Duplicates Are Not Counted. 
public class WordCount 
{ 
    public static void main(String[]args) 
    { 
     // Initialize Variables 
     String sentence = ""; 
     int wordCount = 1, startingPoint = 0; 


     // Prompt User For Sentence 
     sentence = JOptionPane.showInputDialog(null, "Please input a sentence.", "Input Information Below", 2); 


     // Remove All Punctuations. To Check For More Punctuations Just Add Another Replace Statement. 
     sentence = sentence.replace(",", "").replace(".", "").replace("?", ""); 


     // Convert All Characters To Lowercase - Must Be Done To Compare Upper And Lower Case Words. 
     sentence = sentence.toLowerCase(); 


     // Count The Number Of Words 
     for (int i = 0; i < sentence.length(); i++) 
      if (sentence.charAt(i) == ' ') 
       wordCount++; 


     // Initialize Array And A Count That Will Be Used As An Index 
     String[] words = new String[wordCount]; 
     int count = 0; 


     // Put Each Word In An Array 
     for (int i = 0; i < sentence.length(); i++) 
     { 
      if (sentence.charAt(i) == ' ') 
      { 
       words[count] = sentence.substring(startingPoint,i); 
       startingPoint = i + 1; 
       count++; 
      } 
     } 


     // Put Last Word In Sentence In Array 
     words[wordCount - 1] = sentence.substring(startingPoint, sentence.length()); 


     // Put Array Elements Into A Set. This Will Remove Duplicates 
     HashSet<String> wordsInSet = new HashSet<String>(Arrays.asList(words)); 


     // Format Words In Hash Set To Remove Brackets, And Commas, And Convert To String 
     String wordsString = wordsInSet.toString().replace(",", "").replace("[", "").replace("]", ""); 


     // Print Out None Duplicate Words In Set And Word Count 
     JOptionPane.showMessageDialog(null, "Words In Sentence:\n" + wordsString + " \n\n" + 
               "Word Count: " + wordsInSet.size(), "Sentence Information", 2); 
    } 
} 
0

在解析输入字符串时,将它逐字地存储在地图数据结构中。只要确保“单词”,“单词?” “字!”所有这些都与地图中的关键词“单词”一起存储,并且无论何时必须添加到地图中,都会增加单词的计数。