Results 1 to 11 of 11
Thread: Easy question
- 08-03-2007, 09:20 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 10
- Rep Power
- 0
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:
Java 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);} } }
JNLast edited by levent; 08-03-2007 at 09:34 PM. Reason: Code placed inside [code] tag.
- 08-03-2007, 09:36 PM #2levent Guestif (textline = "William") System.out.println("xxxxxx");
More than that, for comparing strings, you should use equals method of String class!
- 08-03-2007, 09:56 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 10
- Rep Power
- 0
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
- 08-03-2007, 09:59 PM #4levent Guest
Check javadoc of String class.
You can write that line like this:
Java Code:if (textline.equals("William")) System.out.println("xxxxxx");
- 08-03-2007, 10:10 PM #5
Member
- Join Date
- Aug 2007
- Posts
- 10
- Rep Power
- 0
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?
- 08-03-2007, 10:12 PM #6
Member
- Join Date
- Aug 2007
- Posts
- 10
- Rep Power
- 0
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?
- 08-03-2007, 10:14 PM #7levent GuestJava Code:
... while((textline = buffer.readLine()) !=null) System.out.println(textline); if (textline.equals("William")) System.out.println("xxxxxx"); ...
Java 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 10:17 PM.
- 08-03-2007, 10:17 PM #8levent Guestalso, 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?
Read the javadoc i sent. There is a indexOf method there if you want to find all lines containing "william".
- 08-03-2007, 10:23 PM #9
Member
- Join Date
- Aug 2007
- Posts
- 10
- Rep Power
- 0
Originally Posted by JavaNoob;
- 08-03-2007, 10:25 PM #10levent Guest
Check my previous post: http://www.java-forums.org/new-java/....html#post5353
- 08-03-2007, 10:28 PM #11
Member
- Join Date
- Aug 2007
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
JSP Question
By maheshkumarjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 10:51 AM -
help me with a realy easy program (substring)
By michcio in forum New To JavaReplies: 7Last Post: 01-27-2008, 12:41 AM -
Need help with a question please
By sonal in forum New To JavaReplies: 1Last Post: 11-29-2007, 09:17 PM -
easy way to study the java springs concept
By kumar84 in forum New To JavaReplies: 1Last Post: 07-17-2007, 03:53 PM
Bookmarks