|
PLz i really need help on this final thing
I want to be able to read strings from a file, and search if a word im looking for is in there. if it is, a message pane will pop up saying yes. if not, then it will say no. Right now, i can't seem to get the two methods to connect. THeres always an error.
Code:
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.swing.*;
public class AccessFile {
public String phrase;
public void displayFile(String f) throws IOException {
String phrase = f;
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
f = br.readLine();
while (f != null) {
System.out.println(f);
f = br.readLine();
}
br.close();
fr.close();
}
public void search(){
UIManager.put("OptionPane.background", Color.white);
UIManager.put("Panel.background", Color.white);
String word = JOptionPane.showInputDialog (null, "Please enter the word you are looking for", "Find", JOptionPane.OK_CANCEL_OPTION);
StringTokenizer st = new StringTokenizer(phrase);// Created a new object, that will allow me to tokenize String.
/** Basically, boolean starts out as false. if the StringTokenizer st has more token, it will keep
* reading each word. If one of the words in the string equal to the word the user entered, then
* boolean will turn true.
*/
boolean compare = false;
while (st.hasMoreTokens())
{
if (st.nextToken().equals (word))
compare = true;
}
/** If boolean was true, then it will show up the message screen saying word found. If boolean isn't true, then
* a screen will pop up saying you sure you entered it correctly.
*/
if (compare){
JOptionPane.showMessageDialog (null, "Word Found.", "Word is found!", JOptionPane.PLAIN_MESSAGE);
System.out.println((word));
}
else{
JOptionPane.showMessageDialog (null, "You sure you entered it correctly?", "Word is not found!", JOptionPane.PLAIN_MESSAGE);
}
}
}
Error:
NullPointerException:
at java.util.StringTokenizer.<init>(Unknown Source)
at java.util.StringTokenizer.<init>(Unknown Source)
at AccessFile.search(AccessFile.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
>
PLz help. i really need it. Some tips, editing of the code, etc.
|