Results 1 to 6 of 6
  1. #1
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default BufferedReader problem

    Hey

    i am trying to read txt file, which says
    hello bye
    hello bye
    hello bye

    The code goes through except when i println the str array indexes in which the lines are stored i get the output
    hello bye
    ello bye
    ello bye

    so it cuts of the first letter of every line except for the first one, any ideas?
    here is my code

    PHP Code:
    int y = 0;
            try {
                BufferedReader br = new BufferedReader(new FileReader("Desktop/read.txt"));
                do {
                    str[y] = br.readLine();
                    y++;
                } while (br.read() != -1);
                System.out.println(str[0]);
                System.out.println(str[1]);
                System.out.println(str[2]);
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your while test where you call br.read() clips off the h due to the read() call. The tutorials will tell you how to do this better.

    Something like:

    Java Code:
    List<String> stringList = new ArrayList<String>();
    while ((String line = br.readLine()) != -null) {
        stringList.add(line);
    }
    
    for (String line; stringList) {
        System.out.println(line);
    }
    Last edited by Fubarable; 07-05-2010 at 08:12 AM.

  3. #3
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks, i'll try it out

    EDIT
    just tried it and it works perfectly, thank you for the quick reply
    Last edited by dilpreet28; 07-05-2010 at 08:26 AM.

  4. #4
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    i have another question
    i have an array with usernames and passwords that are in a 2d array, and i check those values with the text entered from a textField but it never works. here is the code -

    String username = username_txt.getText().toLowerCase();
    for (int v = 0; v < user[0].length; v++) {
    if (user[0][v] != null) {
    if (username == user[0][v]) {
    userCorrect = true;
    }
    }
    }
    for example user[0][0] = admin, and if i enter that in the text field it still doesn't run the if statement. I have printed out both the values and they are the same but it doesn't work, any ideas?

    EDIT

    don't worry i figured it out, i used the compare property to compare both the strings and checked if the value was equal to 0 and went from there
    Last edited by dilpreet28; 07-05-2010 at 09:39 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    That's what equals() is for.
    compare really isn't the correct choice for this.

    Anyway, in short, '==' is for primitives. If you want to determine equality for an Object (which is what String is) then you want to use equals().

  6. #6
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    yea, first i used compareTo and then i looked at the properties again and found equals().
    Thanks for pointing it out

Similar Threads

  1. BufferedReader
    By Nerijus in forum New To Java
    Replies: 4
    Last Post: 04-26-2010, 07:28 PM
  2. BufferedReader, need help!
    By zacharyrod in forum New To Java
    Replies: 10
    Last Post: 11-19-2009, 10:56 AM
  3. problem with BufferedReader
    By Ozmosis in forum Networking
    Replies: 6
    Last Post: 10-29-2009, 09:07 PM
  4. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 01:57 PM
  5. Replies: 1
    Last Post: 01-15-2008, 08:55 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
  •