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);
}
}
This is my current code. I can't figure out how to To test whether a given string represents a number or not, I need to have a try block in which I pass each string in the file to the Double.parseDouble(). I want to return each number in the text file, as well as the string before and after it.