작성하려는 프로그램은 사용자 정의 파일에서 단어를 가져 와서 해당 단어를 변수로 저장 한 다음 다른 사용자 정의 파일에서 해당 단어를 검색하는 프로그램입니다 단어, 거기에 출력 위치.외부 파일에서 여러 단어 동시 검색 (Java)
프로그램은 프로그램이 단어를 가져 와서 변수로 저장하는 지점까지 작동합니다. 프로그램의 문제점은 검색 메소드가 널 (null) 결과를 리턴한다는 것입니다. 필자의 주된 의문은 검색 메서드의 코드가 read 메서드의 코드와 호환되지 않거나 두 메서드가 동시에 실행되지 않는다는 것입니다.
검색 방법은 검색 클래스에 있고 읽기 메서드는 읽기 클래스에 있습니다.
여기 내 코드 3 개가 모두 포함되어 있습니다. 모든 수입에 대해 변상을 부탁드립니다.
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());
}
}
}
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();
}
}
이 제 클래스이다
이 제 클래스이다.
감사합니다.
팔로우 규칙을 호출합니다. 그들은 당신의 코드를 읽기 쉽게 만든다. – tucuxi
또한 여러 클래스를 하나의 코드 스 니펫에 붙여 넣습니다. 몇 가지 발췌 문장을 만들거나 추가 클래스를 정적 내부 클래스로 변환하십시오. – tucuxi
팁 주셔서 감사합니다. 나는 프로그래밍과이 사이트에 처음 온 사람이다. 나는 가변 자본화를 제외하고는 그에 따라 조정을했다. 각 클래스를 분리하여 놓으면 코드를 쉽게 볼 수 있습니까? – Digitalwolf