# HDU2072 count the number of words in the article

Problem Description
lily's good friend xiaoou333 is very empty recently. He thought about a meaningless thing, which is to count the total number of different words in an article. Your next task is to help xiaoou333 solve this problem.

Input
There are multiple groups of data, one row for each group, and each group is a small article. Every article is made up of lowercase letters and spaces. There is no punctuation mark. When you encounter ා, it means the end of input.

Output
Each group outputs only one integer, which is in a separate line. The integer represents the total number of different words in an article.

Sample Input
you are my friend

Sample Output
4

The java code is as follows:

```import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
public void run(){
Scanner sc = new Scanner(System.in);
int[] array = new int[1000];
Arrays.fill(array,0);
String input;
int curResult;
int count = -1;
boolean flag = false;
//while ((sc.hasNextLine()) && ((input = sc.nextLine()).trim().charAt(0) != '#')){
while (sc.hasNextLine()){
input = sc.nextLine();
if(input.equals("#")){
break;
}
if(input.contains("#")){
flag = true;
}
curResult = calculate(input);
count++;
//  System.out.println("count + 1) +" the result of the second calculation is "+ curresult";
array[count] = curResult;
if(flag == true){
break;
}
}
for (int i = 0; i <= count ; i++) {
System.out.println(array[i]);
}
//System.out.print(array[count]);
}
public int calculate(String str) {
String str1 = str.trim();
if(str1.length() == 0){
return 0;
}
// String str1 = str.trim();
String[] strArray = str1.split("\\s+");
Set<String> set = new HashSet<>();
//   System.out.println("the set size at this time is" + set.size());
//int count = 0;
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].contains("#")){
break;
}
if (!set.contains(strArray[i])) {
// System.out.println("set at this time is" + set);
//count++;
}
}
//        if(set.isEmpty()){
//            return 0;
//        }
return set.size();
}
public static  void main(String[] args){
new Main().run();
// System.out.println(new Main().calculate("      #"));
}
}

```

matters needing attention:
1. The calculate() function should return 0 when entering a series of spaces ("");

```String str1 = str.trim();
if(str1.length() == 0){
return 0;
}
```

2. Enter a series of spaces followed by "ා", it should return 0 and the input is finished;
3. Input "my love my baby" ා "and you", (add some spaces before, after and in the middle), it should return 2, and the input is finished; (note that the flag is judged to be true here and break must be after the array is changed, otherwise the information of this line will not be output) (the judgment will be finished after encountering "#" in the middle, and the number of set words counted before will be returned.)

```while (sc.hasNextLine()){
input = sc.nextLine();
if(input.equals("#")){
break;
}
if(input.contains("#")){
flag = true;
}
curResult = calculate(input);
count++;
//  System.out.println("count + 1) +" the result of the second calculation is "+ curresult";
array[count] = curResult;
if(flag == true){
break;
}
}
```

4. Input "ා", the program has no output, and the input is finished;
5. The problem is not difficult. The difficulty lies in the thorough consideration.
Another method is attached: (also AC, but for test cases of the same nature, the output is obviously different, in fact, I don't quite understand)
The code is as follows:

```import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main1{

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();

if(str.equals("#")){
break;
}
String strs[] = str.split(" +");
Map<String, String> map = new HashMap<String, String>();
for(int i=0;i<strs.length;i++){
map.put(strs[i], strs[i]);
}
System.out.println(map.size());
}
}

}
```

The output is as follows:

```  my   love my baby#sjs
4

```
Published 7 original articles, won praise 1, visited 629

Tags: Java

Posted on Thu, 06 Feb 2020 02:07:20 -0500 by Distant_storm