Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

java - Word Count no duplicates

Here is my word count program using java. I need to reprogram this so that something, something; something? something! and something count as one word. That means it should not count the same word twice irregardless of case and punctuation.

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 ");
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since this is homework, here are some hints and advice.

  • There is a clever little method called String.split that splits a string into parts, using a separator specified as a regular expression. If you use it the right way, this will give you a one line solution to the "word count" problem. (If you've been told not to use split, you can ignore that ... though it is the simple solution that a seasoned Java developer would consider first.)

  • Format / indent your code properly ... before you show it to other people. If your instructor doesn't deduct marks for this, he / she isn't doing his job properly.

  • Use standard Java naming conventions. The capitalization of Lines is incorrect. It could be LINES for a manifest constant or lines for variable, but a mixed case name starting with a capital letter should always be a class name.

  • Be consistent in your use of white space characters around operators (including the assignment operator).

  • It is a bad idea (and completely unnecessary) to hard wire the number of lines of input that the user must supply. And you are not dealing with the case where he / supplies less than 6 lines.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...