我試圖創建的程序是一個程序,它從用戶定義的文件中提取單詞,將這些單詞保存爲變量,然後針對這些單詞搜索不同的用戶定義文件字,輸出那裏的位置。同時搜索外部文件中的多個單詞(Java)
該程序工作到幷包括程序獲取單詞並將它們保存爲變量的點。該程序的問題在於搜索方法返回空結果。我主要懷疑的是,搜索方法中的代碼與讀取方法中的代碼不兼容,或者這兩種方法沒有同時運行。
搜索方法在搜索類中,讀取方法在閱讀類中。
這裏是我的代碼(包含我所有的3個類),請原諒所有的進口。
這是第一類:
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Combination{
public static void main(String[] args) throws FileNotFoundException{
Scanner userInput = new Scanner(System.in);
Reading ReadingObject = new Reading();
System.out.println("Please enter the file that you wish to open");
String temp = userInput.nextLine();
ReadingObject.setFileName(temp);
ReadingObject.read();
Scanner searchForWord = new Scanner(System.in);
Searching SearchingObject = new Searching();
System.out.println("Please enter the file that you would like to search for these words in");
String temp1 = searchForWord.nextLine();
SearchingObject.setFileName(temp1);
SearchingObject.search();
}
}
這是第二類:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
class Reading {
private String file;
public void setFileName(String fileName){
file = fileName;
}
public String getFileName(){
return file;
}
public void read(){
try{
//Choosing the file to open
FileInputStream fstream = new FileInputStream(getFileName());
//Get the object of datainputstream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null;
//Read the file line by line
while((strLine = br.readLine()) != null){
// \\s+ means any number of whitespaces between tokens
String [] tokens = strLine.split("\\s+");
String [] words = tokens;
for(String word : words){
System.out.print(word);
System.out.print(" ");
Searching SearchingObject = new Searching();
SearchingObject.setWord(word);
}
System.out.print("\n");
}
in.close();
}
catch(Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
這是第三類:
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Searching {
private String file1;
public void setFileName(String fileName){
file1 = fileName;
}
public String getFileName(){
return file1;
}
private String word1;
public void setWord(String wordName){
word1 = wordName;
}
public String getWord(){
return word1;
}
public void search() throws FileNotFoundException{
try{
//Choosing the file to open
FileInputStream fstream = new FileInputStream(getFileName());
//Get the object of datainputstream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null;
while((strLine = br.readLine()) != null){
Pattern p = Pattern.compile(getWord());
Matcher m = p.matcher(strLine);
int start = 0;
while (m.find(start)) {
System.out.printf("Word found: %s at index %d to %d.%n", m.group(), m.start(), m.end());
start = m.end();
}
}
}
catch(Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
任何幫助將不勝感激。
Regards
按照約定:ClasNamesStartWithCaps,variablesDoNot。它們使您的代碼更易於閱讀。 – tucuxi
此外,您正在將幾個類粘貼到一個代碼片段中。要麼創建幾個片段,要麼將額外的類轉換爲靜態內部類。 – tucuxi
感謝您的提示。我是編程和本網站的新手。除可變資本化外,我已作出相應調整。如果我將每個類分開放置,代碼是否易於查看? – Digitalwolf