Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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-03-2007, 10:20 PM
Member
 
Join Date: Aug 2007
Posts: 10
JavaNoob is on a distinguished road
Easy question
Ok I am trying to write a program that when ran, will Println all the lines in a specific file (ss.txt). while println-ing, f textline is ever a certain name "william", it will also println "xxxxxxxx" (so that I can find that specific name when I look at the list ).

I run my program and it traverses the file and prints that lines correctly but it never does anything once it gets to the "william" name.

here is my code:

Code:
import java.io.*; public class ReadFile { public static void main(String[] args) { try { FileReader file = new FileReader("data/ss.txt"); BufferedReader buffer=new BufferedReader (file); String textline = null; while((textline = buffer.readLine()) !=null) System.out.println(textline); if (textline = "William") System.out.println("xxxxxx"); buffer.close(); } catch (IOException e) { System.out.println(e);} } }
Thanks
JN

Last edited by levent : 08-03-2007 at 10:34 PM. Reason: Code placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-03-2007, 10:36 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Quote:
if (textline = "William") System.out.println("xxxxxx");
There should be two equals sign there to compare the result.

More than that, for comparing strings, you should use equals method of String class!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-03-2007, 10:56 PM
Member
 
Join Date: Aug 2007
Posts: 10
JavaNoob is on a distinguished road
Levent, thanks for the quick response!
I will give that a try (though i may have tried that).
what is this "equals method of String class" (Keep in mind, I am relatively new to Java)?

Thanks
JN
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-03-2007, 10:59 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Check javadoc of String class.

You can write that line like this:

Code:
if (textline.equals("William")) System.out.println("xxxxxx");
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-03-2007, 11:10 PM
Member
 
Join Date: Aug 2007
Posts: 10
JavaNoob is on a distinguished road
I tried the "textline.equals("william") and it still ran through the txt file without println-ing the "xxxxx" . At the end, it did say:
Exception in thread "main" java.lang.NullPointerException
at ReadFile.main(ReadFile.java:13)

line 13 is where it if command is.

any more ideas?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-03-2007, 11:12 PM
Member
 
Join Date: Aug 2007
Posts: 10
JavaNoob is on a distinguished road
do you think it's returning this argument because it's not finding anything named "william"?

also, is there a chance that it's looking for a string that is JUST william but it's not finding it because my string has a william and a last name associated to it?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-03-2007, 11:14 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Code:
... while((textline = buffer.readLine()) !=null) System.out.println(textline); if (textline.equals("William")) System.out.println("xxxxxx"); ...
You should write those lines like this. I guess you will notice the difference.

Code:
... while((textline = buffer.readLine()) !=null) { System.out.println(textline); if (textline.equals("William")) System.out.println("xxxxxx"); } ...

Last edited by levent : 08-03-2007 at 11:17 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-03-2007, 11:17 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Quote:
also, is there a chance that it's looking for a string that is JUST william but it's not finding it because my string has a william and a last name associated to it?
Yes, it is looking for exactly "william" in a case sensitive way.

Read the javadoc i sent. There is a indexOf method there if you want to find all lines containing "william".
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-03-2007, 11:23 PM
Member
 
Join Date: Aug 2007
Posts: 10
JavaNoob is on a distinguished road
Quote:
Originally Posted by JavaNoob;
also, is there a chance that it's looking for a string that is JUST william but it's not finding it because my string has a william and a last name associated to it?
I put that to the test, and made a line that only had the name william, and it still didn't work.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-03-2007, 11:25 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Check my previous post: Easy question
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 08-03-2007, 11:28 PM
Member
 
Join Date: Aug 2007
Posts: 10
JavaNoob is on a distinguished road
Perfect! That's what I was looking for!

Thanks again.


JN
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
JSP Question maheshkumarjava JavaServer Pages (JSP) and JSTL 1 03-29-2008 11:51 AM
help me with a realy easy program (substring) michcio New To Java 7 01-27-2008 01:41 AM
Need help with a question please sonal New To Java 1 11-29-2007 10:17 PM
easy way to study the java springs concept kumar84 New To Java 1 07-17-2007 04:53 PM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM


All times are GMT +3. The time now is 05:16 PM.


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