Results 1 to 11 of 11

Thread: Easy question

  1. #1
    JavaNoob is offline Member
    Join Date
    Aug 2007
    Posts
    10
    Rep Power
    0

    Default 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);}
    }
    }
    Thanks
    JN
    Last edited by levent; 08-03-2007 at 10:34 PM. Reason: Code placed inside [code] tag.

  2. #2
    levent Guest

    Default

    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!

  3. #3
    JavaNoob is offline Member
    Join Date
    Aug 2007
    Posts
    10
    Rep Power
    0

    Default

    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

  4. #4
    levent Guest

    Default

    Check javadoc of String class.

    You can write that line like this:

    Java Code:
    if (textline.equals("William")) System.out.println("xxxxxx");

  5. #5
    JavaNoob is offline Member
    Join Date
    Aug 2007
    Posts
    10
    Rep Power
    0

    Default

    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?

  6. #6
    JavaNoob is offline Member
    Join Date
    Aug 2007
    Posts
    10
    Rep Power
    0

    Default

    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?

  7. #7
    levent Guest

    Default

    Java 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.

    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 11:17 PM.

  8. #8
    levent Guest

    Default

    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".

  9. #9
    JavaNoob is offline Member
    Join Date
    Aug 2007
    Posts
    10
    Rep Power
    0

    Default

    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.

  10. #10
    levent Guest

    Default

    Check my previous post: Easy question

  11. #11
    JavaNoob is offline Member
    Join Date
    Aug 2007
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:51 AM
  3. help me with a realy easy program (substring)
    By michcio in forum New To Java
    Replies: 7
    Last Post: 01-27-2008, 01:41 AM
  4. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 10:17 PM
  5. easy way to study the java springs concept
    By kumar84 in forum New To Java
    Replies: 1
    Last Post: 07-17-2007, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •