Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用扫描器定界符(包括Java中的单引号或撇号)从文本文件中过滤出非字母_Java_Java.util.scanner_Delimiter - Fatal编程技术网

如何使用扫描器定界符(包括Java中的单引号或撇号)从文本文件中过滤出非字母

如何使用扫描器定界符(包括Java中的单引号或撇号)从文本文件中过滤出非字母,java,java.util.scanner,delimiter,Java,Java.util.scanner,Delimiter,请我要对文件中的每个单词进行计数,并且计数不应包括撇号、逗号、句号、问号、感叹号、e.t.c.等非字母,即字母表中的字母。 我试图使用这样的分隔符,但它不包括撇号 Scanner fileScanner = new Scanner("C:\\MyJavaFolder\\JavaAssignment1\\TestFile.txt"); int totalWordCount = 0; //Firstly to count all the words in the file with

请我要对文件中的每个单词进行计数,并且计数不应包括撇号、逗号、句号、问号、感叹号、e.t.c.等非字母,即字母表中的字母。 我试图使用这样的分隔符,但它不包括撇号

Scanner fileScanner = new Scanner("C:\\MyJavaFolder\\JavaAssignment1\\TestFile.txt");
    int totalWordCount = 0;

    //Firstly to count all the words in the file without the restricted characters 
    while (fileScanner.hasNext()) {
        fileScanner.useDelimiter(("[.,:;()?!\" \t\n\r]+")).next();
        totalWordCount++;
    }
    System.out.println("There are " + totalWordCount + " word(s)");

  //Then later I create an array to store each individual word in the file for counting their lengths.
    Scanner fileScanner2 = new Scanner("C:\\MyJavaFolder\\JavaAssignment1\\TestFile.txt");
    String[] words = new String[totalWordCount];
    for (int i = 0; i < totalWordCount; ++i) {
        words[i] = fileScanner2.useDelimiter(("[.,:;()?!\" \t\n\r]+")).next();
    }
Scanner fileScanner=new Scanner(“C:\\MyJavaFolder\\JavaAssignment1\\TestFile.txt”);
int totalWordCount=0;
//首先统计文件中的所有单词,不包含限制字符
while(fileScanner.hasNext()){
fileScanner.useDelimiter((“[,:;()?!\”\t\n\r]+”).next();
totalWordCount++;
}
System.out.println(“有“+totalWordCount+”个字)”;
//然后,我创建一个数组来存储文件中的每个单词,以计算它们的长度。
Scanner fileScanner2=新扫描仪(“C:\\MyJavaFolder\\JavaAssignment1\\TestFile.txt”);
String[]words=新字符串[totalWordCount];
for(int i=0;i
这好像不管用


请告诉我如何执行此操作?

分隔符不是正则表达式,因此在您的示例中,它正在查找在“[,:;()?!\”\t\n\r]+”之间拆分的内容

您可以使用regexp而不是分隔符

将regexp类与group方法一起使用可能是您想要的

String pattern = "(.*)[.,:;()?!\" \t\n\r]+(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(test);
    if (m.find( )) {
        System.out.println("Found value: " + m.group(1) );
    }

玩这些类,你会发现它与你需要的更为相似

在我看来,你不想使用空格和结束线以外的任何东西进行过滤“如果您使用‘过滤字数’,则将返回两个单词。以下是如何更改原始代码以使其正常工作

Scanner fileScanner = new Scanner(new File("C:\\MyJavaFolder\\JavaAssignment1\\TestFile.txt"));
    int totalWordCount = 0;
    ArrayList<String> words = new ArrayList<String>();

    //Firstly to count all the words in the file without the restricted characters 
    while (fileScanner.hasNext()) {
        //Add words to an array list so you only have to go through the scanner once
        words.add(fileScanner.next());//This defaults to whitespace
        totalWordCount++;
    }
    System.out.println("There are " + totalWordCount + " word(s)");
    fileScanner.close();

已经测试了这段代码,它似乎对我有用。
replaceAll
,根据使用正则表达式进行匹配,因此它应该匹配这些字符中的任何一个,并从本质上删除它。

您可以在分隔符中尝试此正则表达式:
fileScanner.useDelimiter((“[^a-zA-Z]|[^']”)

这将使用任何非字母字符或非撇号作为分隔符。这样,您的单词将包括撇号,但不包括任何其他非字母字符


然后,你必须循环遍历每个单词,检查撇号,如果你想让长度准确,就要解释它们。您可以删除每个撇号,长度将与单词中的字母数匹配,或者您可以创建具有自己的长度字段的单词对象,这样您就可以按原样打印单词,并知道该单词中的字母字符数。

哦,我不知道分隔符只是为了查找拆分的单词。现在知道了这一点,我认为我的问题应该重新表述为“我如何计算一个单词中由撇号分隔的字母”,例如,“It's”是一个3个字母的单词,但使用分隔符,它将其分别转换为长度为2和长度为1的两个单词。换句话说,我如何在计算单词时跳过撇号?非常好的东西,尤其是在arraylist中…我没有想到这一点,现在知道了,我想我的问题应该改为“如何计算一个单词中由撇号分隔的字母”,例如,“It's”是一个3个字母的单词,但使用分隔符,它将其分别转换为长度为2和长度为1的两个单词。换句话说,我如何在计算单词时跳过撇号呢?我已经编辑了代码,所以应该可以解决您的问题。我已经测试过了,它似乎对我有用。这个问题回答了你的问题吗?这个问题被重新表述为“我如何计算一个单词中由撇号分隔的字母数”,例如,“it's”是一个3个字母的单词,但使用分隔符,它会将其分别转换为长度为2和长度为1的两个单词。换句话说,在计算单词时,我如何跳过撇号呢?
int totalLetters = 0;
int[] lettersPerWord = new int[words.size()];
for (int wordNum = 0; wordNum < words.size(); wordNum++)
{
 String word = words.get(wordNum);
 word = word.replaceAll("[.,:;()?!\" \t\n\r\']+", "");
 lettersPerWord[wordNum] = word.length();
 totalLetters = word.length();
}