将一个字符串拆分成一个要再次调用的字数组

时间:2021-01-22 15:45:27

This is my code so far below

这是我目前的代码

import java.util.*;
import java.io.*;
public class USconstitution   
{
  public static void main(String [] args) throws Exception
  {
     Scanner inFile = new Scanner(new File("constitution.txt"));
     int x = 0;
     int keyword1 = 0;
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Enter a word from the keyboard");
     String input = keyboard.next();
     while(inFile.hasNext())
     {
        String word = inFile.next();
        x++;
        if(input.equalsIgnoreCase(word))
           keyword1++;
     }
     //System.out.println("\n" + x);
     System.out.println("The constitution has " + x + " words");
     System.out.println("The first keyword shows up " + keyword1 + " times in the  

     constitution");
  }
}

THE OUTPUT SO FAR =

输出SO FAR =

Enter a word from the keyboard

从键盘输入一个单词

President

主席

The constitution has 4610 words

宪法有4610个字

The first keyword shows up 20 times in the constitution

第一个关键词在宪法中出现了20次


My goal for this program is to search through a text file that has been given to me which contains the US Constitution.

我的这个计划的目标是搜索一个包含美国宪法的文本文件。

The first part simply counts how many words are in the text file, the next bit that I am trying to do is allow people to search for certain keywords and have it search the text file and say how many times that word shows up.

第一部分只计算文本文件中有多少单词,我尝试做的下一位是允许人们搜索某些关键字并让它搜索文本文件并说出该单词显示的次数。

I was thinking of having the prompt ask what keywords the user wishes to enter and have it use the split method to create each word as an individual string and search for that in the file then output how many times it appears. Although I am not so sure how to go about this, any help would be greatly appreciated!

我想提示询问用户希望输入哪些关键字并让它使用split方法将每个单词创建为单个字符串并在文件中搜索它然后输出它出现的次数。虽然我不太确定如何解决这个问题,但我们将非常感谢您的帮助!

2 个解决方案

#1


1  

Try this:

尝试这个:

public static void main(String[] args)
{       
    String text = "the quick brown fox jumps fox fox over the lazy dog brown";
    String[] textArray = text.split(" ");
    String[] keysToSearch = {"the", "fox", "dog"};
    int count = 0;
    System.out.println(text);

    for(String key: keysToSearch)
    {                       
        for(String s : textArray)
        {
            if(key.equals(s))
            {
                count++;
            }               
        }
        System.out.println("Count of ["+key+"] is : "+count);
        count=0;
    }
}

Output:

输出:

the quick brown fox jumps fox fox over the lazy dog brown
Count of [the] is : 2
Count of [fox] is : 3
Count of [dog] is : 1

#2


0  

Take the code that does the check out of the main method and put it in a separate method that takes a single keyword as a parameter. Then you can split the input string given from the console using

获取检出main方法的代码,并将其放在一个单独的方法中,该方法将一个关键字作为参数。然后,您可以使用分割从控制台给出的输入字符串

String[] keywords = input.split(" "); // will split at spaces

Then all you have to do is loop through all of the keywords in your array

然后,您所要做的就是遍历数组中的所有关键字

for(int  i = 0; i < keywords.length; i++){

    yourCountMethod(keywords[i]);

}

This is just the general idea, obviously you will have to change the code to suit.

这只是一般的想法,显然你将不得不改变代码以适应。

Hope that helps :)

希望有所帮助:)

#1


1  

Try this:

尝试这个:

public static void main(String[] args)
{       
    String text = "the quick brown fox jumps fox fox over the lazy dog brown";
    String[] textArray = text.split(" ");
    String[] keysToSearch = {"the", "fox", "dog"};
    int count = 0;
    System.out.println(text);

    for(String key: keysToSearch)
    {                       
        for(String s : textArray)
        {
            if(key.equals(s))
            {
                count++;
            }               
        }
        System.out.println("Count of ["+key+"] is : "+count);
        count=0;
    }
}

Output:

输出:

the quick brown fox jumps fox fox over the lazy dog brown
Count of [the] is : 2
Count of [fox] is : 3
Count of [dog] is : 1

#2


0  

Take the code that does the check out of the main method and put it in a separate method that takes a single keyword as a parameter. Then you can split the input string given from the console using

获取检出main方法的代码,并将其放在一个单独的方法中,该方法将一个关键字作为参数。然后,您可以使用分割从控制台给出的输入字符串

String[] keywords = input.split(" "); // will split at spaces

Then all you have to do is loop through all of the keywords in your array

然后,您所要做的就是遍历数组中的所有关键字

for(int  i = 0; i < keywords.length; i++){

    yourCountMethod(keywords[i]);

}

This is just the general idea, obviously you will have to change the code to suit.

这只是一般的想法,显然你将不得不改变代码以适应。

Hope that helps :)

希望有所帮助:)