简体   繁体   English

使扫描仪读取文本文件

[英]Getting scanner to read text file

I am trying to use a scanner to read a text file pulled with JFileChooser . 我正在尝试使用扫描仪读取使用JFileChooser拉出的文本文件。 The wordCount is working correctly, so I know it is reading. wordCount工作正常,所以我知道它正在读取。 However, I cannot get it to search for instances of the user inputted word. 但是,我无法搜索用户输入的单词的实例。

public static void main(String[] args) throws FileNotFoundException {
    String input = JOptionPane.showInputDialog("Enter a  word");
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.showOpenDialog(null);
    File fileSelection = fileChooser.getSelectedFile();
    int wordCount = 0;
    int inputCount = 0;
    Scanner s = new Scanner (fileSelection);
    while (s.hasNext()) {
        String word = s.next();
        if (word.equals(input)) {
            inputCount++;
    }
    wordCount++;
}

如果用户输入的文本大小写不同,则应尝试使用equalsIgnoreCase()

除了blackpanthers答案,您还应该使用trim()来解决空白问题。as“ abc”不等于“ abc”

You'll have to look for 您必须寻找

, ; ,; . ! ? etc. 等等

for each word. 每个字。 The next() method grabs an entire string until it hits an empty space . next()方法将捕获整个字符串,直到遇到empty space为止。

It will consider "hi, how are you?" 它将考虑“嗨,你好吗?” as the following "hi,", "how", "are", "you?". 如以下“嗨”,“如何”,“是”,“您”?

You can use the method indexOf(String) to find these characters. 您可以使用方法indexOf(String)查找这些字符。 You can also use replaceAll(String regex, String replacement) to replace characters. 您还可以使用replaceAll(String regex,String replacement)替换字符。 You can individuality remove each character or you can use a Regex , but those are usually more complex to understand. 您可以个性化地删除每个字符,也可以使用Regex ,但是这些字符通常更难于理解。

//this will remove a certain character with a blank space
word = word.replaceAll(".","");
word = word.replaceAll(",","");
word = word.replaceAll("!","");
//etc.

Read more about this method: 阅读有关此方法的更多信息:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29 http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29

Here's a Regex example: 这是一个正则表达式示例:

//NOTE:  This example will not work for you.  It's just a simple example for seeing a Regex.
//Removes whitespace between a word character and . or ,
String pattern = "(\\w)(\\s+)([\\.,])";
word = word.replaceAll(pattern, "$1$3"); 

Source: 资源:

http://www.vogella.com/articles/JavaRegularExpressions/article.html http://www.vogella.com/articles/JavaRegularExpressions/article.html

Here is a good Regex example that may help you: 这是一个很好的Regex示例,可以为您提供帮助:

Regex for special characters in java 正则表达式用于Java中的特殊字符

Parse and remove special characters in java regex 解析并删除Java正则表达式中的特殊字符

Remove all non-"word characters" from a String in Java, leaving accented characters? 从Java中的字符串中删除所有非单词字符,留下带重音符号的字符?

You should take a look at matches() . 您应该看一下matches()

equals will not help you, since next() doesn't return the file word by word, but rather whitespace ( not comma, semicolon, etc.) separated token by token (as others mentioned). equals不会对您有所帮助,因为next()不会逐字返回文件,而是将空格( 不是逗号,分号等)逐个标记(如其他提到的)分隔开。

Here the java doc 这是java文档
String#matches(java.lang.String) String#matches(java.lang.String)

...and a little example. ...还有一个小例子。

input = ".*" + input + ".*";
...
boolean foundWord = word.matches(input)

. is the regex wildcard and stands for any sign. 是正则表达式通配符,代表任何符号。 .* stands for 0 or more undefined signs. .*代表0个或多个未定义符号。 So you get a match, if input is somewhere in word . 如果输入在word某处,那么您将获得匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM