Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2007, 06:54 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-02-2007, 12:24 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.io.*; import java.awt.Color; import javax.swing.*; public class AccessFileRx { public AccessFileRx() { UIManager.put("OptionPane.background", Color.white); UIManager.put("Panel.background", Color.white); // Step-by-step: String fileName = getFileName(); String phrase = getSearchPhrase(); boolean success = searchFile(fileName, phrase); showResult(success, phrase); } private String getFileName() { String name = JOptionPane.showInputDialog (null, "Please enter the " + "name of the file you want to search", "Find", JOptionPane.OK_CANCEL_OPTION); return name; } private String getSearchPhrase() { String word = JOptionPane.showInputDialog (null, "Please enter the " + "word you are looking for", "Find", JOptionPane.OK_CANCEL_OPTION); return word; } private boolean searchFile(String fileName, String s) { boolean found = false; try { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String f = br.readLine(); while (f != null) { // Check for the word or exact phrase. if(f.indexOf(s) != -1) { found = true; break; } f = br.readLine(); } br.close(); fr.close(); } catch(IOException e) { System.err.println("Read error: " + e.getMessage()); } return found; } private void showResult(boolean result, String word) { /** 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 (result){ 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); } } public static void main(String[] args) { new AccessFileRx(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-03-2007, 04:31 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
can u explain it? or document it. im trying to learn java still
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
UJO Framework 0.73.final Java Tip Java Announcements 0 04-15-2008 08:53 PM
Scala 2.7.0-final JavaBean Java Announcements 0 03-02-2008 12:30 AM
problem with final sireesha New To Java 1 11-20-2007 03:31 AM
I have a problem with final sireesha New To Java 1 11-20-2007 02:25 AM
Poi 3.0-final levent Java Announcements 0 05-22-2007 09:05 AM


All times are GMT +3. The time now is 11:23 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org