import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.FileReader;
import java.util.Arrays;
public class Part2{
public static void main(String[] args){
String[] holdPosition = new String[30];
String FileToBeSearched = JOptionPane.showInputDialog("Enter the file name to be searched: ");
String WordToBeSearched = JOptionPane.showInputDialog("Enter the word to be searched: ");
try{
FileReader searchedFile = new FileReader(FileToBeSearched);
Scanner scannedFile = new Scanner(searchedFile);
int i = 0;
String NumOfLines = "";
while(scannedFile.hasNext()){
String currentWord = scannedFile.next();
holdPosition[i] = currentWord;
if(currentWord.equalsIgnoreCase(WordToBeSearched)){
NumOfLines += holdPosition[i-1]+" "+currentWord+" "+scannedFile.next()+"\n";
Arrays.fill(holdPosition, null);
}
i++;
}
scannedFile.close();
JOptionPane.showMessageDialog(null,NumOfLines);
}catch(Exception except){}
System.exit(0);
}
}
これは私の現在のコードです。与えられた文字列が数字を表すかどうかをテストするには、ファイル内の各文字列をDouble.parseDouble()に渡すtryブロックが必要です。私は、テキストファイルの各番号とその前後の文字列を返す必要があります。Javaのテキストファイルから文字列から数字を検索
それで問題は何ですか?あなたはあなたが望むものを手に入れるべきです。 – Nabin
テキストファイルにダブルスがあるかどうかを調べる必要があります。私のテキストファイルは以下の通りです。 "私たちは5人で分割するケーキをスライスしているとします。ケーキ全体のうち33.3%が である大きなスライスを切りました。あなたはまた33.3パーセントのスライスをカットするか、 をもっと公平にし、ケーキの残りの66.6パーセントを4つの部分に分割しますか?どのくらいのスライスをカットしますか?それから、各数字の前に単語を、数字の後に単語を、それぞれ印刷しなければなりません。 – Johnboy
私はあなたの質問を正しく理解すれば疑いがあります。あなたが答えを出したようです。こちらもご覧ください:http://stackoverflow.com/a/5769679/1828296 – lospejos