我只想看一个带有一些解释的示例。我使用什么字符串函数来比较对象,它是否比较每个字符或实际单词而没有任何其他字母?
谢谢
不久前,我尝试为一个项目做与这个问题非常相似的事情。在 Java 中有很多方法可以做到这一点,但我使用了 Scanner 类和 File 类。
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); //Just a normal scanner
System.out.println("Please enter in the pathname to the file you want to view.");
String pathname = input.nextLine(); //Pathname to text file
File book = new File(pathname); //Creating a new file using the pathname
if(book.canRead() == false) //If Java cant read the file, this will pop up
{
System.out.println("Your file cannot be read");
}
else if(book.canRead() == true) //If Java can read the file, then this asks for the word to search for
{
System.out.println("Please enter in the word you wish to search for.");
wordToSearchFor = input.nextLine();
wordCounter(book); //Calls the method
}
System.out.println(wordToSearchFor.toLowerCase() + " appeared " + numOfOccurrences + " times in " + pathname);
}
这是您使用 File 类根据您提供的路径名创建文件的主要方法 EX - C:\Users\alex\Downloads\mobydick.txt 然后我检查您是否可以读取该文件,如果可以的话,我会调用一个方法来分析这本书本身
import java.io.*;
import java.util.Scanner;
public class TextReader
{
private static int numOfOccurrences; //Counter to keep track of the number of occurances
private static String wordToSearchFor; //String field so both methods can access it
/*
* This method takes in the file of the book so the scanner can look at it
* and then does all of the calculating to see if the desired word appears,
* and how many times it does appear if it does appear
*/
public static void wordCounter(File bookInput)
{
try
{
Scanner bookAnalyzer = new Scanner(bookInput); //Scanner for the book
while(bookAnalyzer.hasNext()) //While the scanner has something to look at next
{
String wordInLine = bookAnalyzer.next(); //Create a string for the next word
wordInLine = wordInLine.toLowerCase(); //Make it lowercase
String wordToSearchForLowerCase = wordToSearchFor.toLowerCase();
String wordToSearchForLowerCasePeriod = wordToSearchForLowerCase + ".";
if(wordInLine.indexOf(wordToSearchForLowerCase) != -1 && wordInLine.length() == wordToSearchFor.length())
{
numOfOccurrences++;
}
else if(wordInLine.indexOf(wordToSearchForLowerCasePeriod) != -1 && wordInLine.length() == wordToSearchForLowerCasePeriod.length())
{
numOfOccurrences++;
}
}
}
catch(FileNotFoundException e) //Self explanitory
{
System.out.println("The error is FileNotFoundException - " + e);
System.out.println("This should be impossible to get to because error checking is done before this step.");
}
}
Java中的扫描器可以拿一个File对象来分析,这是我在这个方法中做的第一件事。然后我使用一个 while 循环并询问扫描器是否在当前词之后有一个词。只要有一个字,这就会继续运行。然后我创建一个当前单词的字符串,扫描仪将其用作比较的参考。然后我使用 String 类附带的方法将所有内容变为小写,因为大写和小写字母都很重要。
此方法中的第一个 if 语句使用 String 类中的 indexOf 方法检查扫描器当前拥有的单词是否与您正在搜索的词匹配,该方法获取一些字符串并查看它是否存在于另一个字符串中。if 语句比较还确保所需的字长与书中的字长相同,以防您查找“the”并且它不会将“then”标记为单词,因为它包含“the”。第二个 if 语句做同样的事情,只是在你想要的单词末尾加上一个句点。如果您想加倍努力,您还可以检查感叹号、问号、逗号等,但我决定只检查句点。
每当其中一个 if 语句正确时,我将变量加 1,在扫描仪用完要搜索的单词后,我会打印出某个单词在文本文件中出现的总次数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句