Thanks in advance. =)
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class WorkingFiles {
public static void main(String args[]) throws FileNotFoundException {
// Local variable declarations.
String file_name, search_text;
String data_text = "the quick brown fox jumps over the lazy dog.";
// Input instructions to the console.
System.out.print("Enter filename: ");
// Ask for the file name.
Scanner s = new Scanner(System.in);
file_name = s.next();
// Create the text file with filename from the user.
PrintWriter oFile = new PrintWriter(file_name + ".txt");
// Input for search keyword.
System.out.print("Search for: ");
search_text = s.next();
if(data_text.indexOf(search_text) != -1)
JOptionPane.showMessageDialog(null, "\"" + search_text + "\" found!", "Keyword Found", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "\"" + search_text + "\" not found!", "Keyword Not Found", JOptionPane.ERROR_MESSAGE);
// Close the file.
oFile.close();
System.exit(0);
}
}
EDIT: Sorry about the initialization of the search_text. I just forgot to erase that. My teacher told me that even though the progam will stop, chances are there will be times that it won't. [That's what he meant.]
EDIT: Ok, so the code works. The dialog was just behind my IDE. LOL!
This post has been edited by Ricendithas: 13 July 2009 - 04:05 PM

New Topic/Question
Reply




MultiQuote



|