Results 1 to 16 of 16
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default readLine() for loop

    Hi i have a java app that should read and input data into a database i have nearly managed to separate all the data from the file into what i need however when i try to get some elements with a for loop inside a forloop it only prints the first element fine but misses the second then continues to print all the other elements out correctly VERY STRANGE PLEASE HELP :(
    here is my code
    Java Code:
    String personFile = "C:\\Users\\Billy\\Documents\\Computer science\\computing systems 2\\rndalumni\\rnd_alumni.txt";
    BufferedReader in = new BufferedReader(
    new InputStreamReader(new FileInputStream(personFile)));
    DatabaseAccess databaseAccess = new DatabaseAccess();
    try{
    for (String line = in.readLine(); line != null; line = in.readLine())
    {
    String[] w = line.split(":", 2);
    String username = w[1].trim();
    line = in.readLine();
    w = line.split(":", 2);
    String lastname = w[1].trim();
    line = in.readLine();
    w = line.split(":", 2);
    String firstname = w[1].trim();
    line = in.readLine();
    w = line.split(":", 2);
    String email = w[1].trim();
    line = in.readLine();
    w = line.split(":");
    String password = w[1].trim();
    line = in.readLine();
    w = line.split(":", 2);
    String latitude = w[1].trim();
    line = in.readLine();
    w = line.split(":", 2);
    String longditude = w[1].trim();
    line = in.readLine();
    w = line.split(":", 2);
    double longdituded = Double.parseDouble(longditude);
    double latituded = Double.parseDouble(latitude);
    String imageURL = w[1].trim();
    line = in.readLine();
    for (String schoolLine = in.readLine(); schoolLine != null; line = in.readLine())
    {
        w = line.split(":", 2);
        String school = w[1].trim();
        String [] s = schoolLine.split(",", 2);     
        s = school.split(",", 2);
        String schoolName = s[0].trim();
        String date = s[1].trim();
        System.out.println(schoolName); // prints every element except the second
        System.out.println(date); // prints every element except the second
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    for (String schoolLine = in.readLine(); schoolLine != null; line = in.readLine())
    Check your variables.

  3. #3
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    When i change it to
    Java Code:
    for (String schoolLine = in.readLine(); schoolLine != null; schoolLine = in.readLine())
    i get an infinite loop of the first element :/

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Billywizz View Post
    When i change it to
    Java Code:
    for (String schoolLine = in.readLine(); schoolLine != null; schoolLine = in.readLine())
    i get an infinite loop of the first element :/
    Java Code:
    String schoolName = s[0].trim();
    How exactly are you looping through different elements? From what I see you only use the indexes 0 and 1. Also, there's no infinite loop in the nested for statement as long as your text file is at a reasonable length...

  5. #5
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    why is it missing just the second element (in my first code given), it loops fine for all the other elements?

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Billywizz View Post
    When i change it to
    Java Code:
    for (String schoolLine = in.readLine(); schoolLine != null; schoolLine = in.readLine())
    i get an infinite loop of the first element :/
    Quote Originally Posted by Billywizz View Post
    why is it missing just the second element (in my first code given), it loops fine for all the other elements?
    Define element? I only see one element being used here: [1]. Mind highlighting the "element" you're talking about for me?

  7. #7
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    indexes 0 and 1 represent the different parts of the part of file data i am reading for example of some of the file:

    username: bert0000
    lastname: van Helsing
    firstname: Bert
    email: <Mod Edit: deleted>
    password: <Mod Edit: deleted>
    latitude: 50.935698
    longitude: 1.330554
    image: bert0000.png
    school: Southampton-018, 1970 - 1974
    school: Southampton-048, 1974 - 1975
    school: Southampton-008, 1975 - 1977
    school: Southampton-038, 1977 - 1981
    school: Southampton-031, 1981 - 1985

    i use String [] w for the elements which dont repeat eg. username, firstname, lastname etc. with the split string set at (":",2)
    And the String [s] for the elements of school
    index[0] equaling name index [1] equaling date with splitString(",",2)
    ??
    Last edited by Fubarable; 04-06-2011 at 11:37 AM. Reason: delete email and passwords

  8. #8
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    OutPut:

    Southampton-018
    1970 - 1974
    Southampton-008
    1975 - 1977
    Southampton-038
    1977 - 1981
    Southampton-031
    1981 - 1985

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    So you're saying it doesn't read the second record in the text file?

  10. #10
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Exactly :/ ??

  11. #11
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Instead of using a for loop for things like this, I would recommend using a while loop. While loops definitely make more sense in this situation.

    Also, you need to look at your code more carefully:
    Java Code:
    w = line.split(":", 2);
    when it should be:
    Java Code:
    w = schoolLine.split(":", 2);
    I have your code corrected to the best of my knowledge here (remember, I know nothing about your program):

    Java Code:
    while((String schoolLine=in.readLine()) != null)
    {
        w = schoolLine.split(":", 2);
        String school = w[1].trim();
        String [] s = schoolLine.split(",", 2);     
        s = school.split(",", 2);
        String schoolName = s[0].trim();
        String date = s[1].trim();
        System.out.println(schoolName); // prints every element except the second
        System.out.println(date); // prints every element except the second
    }
    Last edited by Solarsonic; 04-01-2011 at 02:22 AM.

  12. #12
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Thanks for your help Supersonic However when i ran what you just said this was output:
    Southampton-048
    1974 - 1975
    Southampton-008
    1975 - 1977
    Southampton-038
    1977 - 1981
    Southampton-031
    1981 - 1985

    which is every element except the first lol
    Is it just me is is this really strange lol
    ??

  13. #13
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Fixed it i just deleted the last readline above the while
    Thankyou very much supersonic LIFESAVER :) :)

  14. #14
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Billywizz View Post
    Fixed it i just deleted the last readline above the while
    Thankyou very much supersonic LIFESAVER :) :)
    Was about to say that. No problem :)

  15. #15
    jancumps is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    Please remove passwords and email addresses from post 7.
    Last edited by jancumps; 04-05-2011 at 08:29 PM.

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

Similar Threads

  1. Replies: 10
    Last Post: 01-17-2009, 08:30 PM
  2. problem with console.readLine()
    By thatguy in forum New To Java
    Replies: 1
    Last Post: 12-14-2008, 07:40 PM
  3. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 10:44 PM
  4. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:44 AM
  5. BufferedReader: readLine method problems
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2007, 02:27 AM

Posting Permissions

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