Results 1 to 15 of 15
  1. #1
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default Reading files JAVA

    I have this text file composed of this:
    20110504
    Stephen harper,160.0,20.0,20.0
    Carole,160.0,20.3,10.0
    Bob,260.0,20.0,500.0

    How can I make it so that it can read from Stephen Harper(completely skip over "20110504" ?

    Thank you

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you know the line you want to skip is going to be the first line in the file? If you do, just read the line but don't do anything with it.

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

    Default

    Are you sure it is supposed to be skipped. Looks suspiciously like a date.

    You could always test the first char of the line read. If a letter: process it, else if it is a number: ignore it.

  4. #4
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    How do I make it so that it doesn't do anything for the first line?

    This is what i got so far:
    while(trace != null) {
    String[]a = trace.split(",");
    name[i] = (a[0]).trim();
    normalhoursworked[i] = Double.parseDouble((a[1]).trim());
    overtimehoursworked[i] = Double.parseDouble((a[2]).trim());
    bonusPaid[i] = Double.parseDouble((a[3]).trim());

    System.out.println(normalhoursworked[i]+" "+ overtimehoursworked[i] +" "+bonusPaid[i]+" "+name);
    i++;
    trace = buffer.readLine();
    }

  5. #5
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    I get this error when i run it:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at ReadPayroll.readDate(ReadPayroll.java:32)
    at ReadPayroll.main(ReadPayroll.java:17)

  6. #6
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    Yes the first line is the date.
    How can I ignore the first line, Im having trouble with that part.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You get that error because you are reading in the first line and splitting it which only creates an array of one item, you then try to access array indexes which do not exist. As I suggested, you can swallow the first line if you know the first line is the one you are skipping, if you do not know it's the first one, junky gave you a good suggestion.

    Also, junky is right, it sure looks like a date, are you sure you don't want to parse it into one?

  8. #8
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    I know for a fact it is the first line I am skipping.
    should I be using if(trace.charAt(0) ??? ) I dont know how to test it if its a number conveniently. I can write one long if statement checking charAt(0) to all the numbers from 0 to 9. Is there a shorter way to do this?

    How can integrate in the while loop to parse only the first line?

    Sorry Im just really new to java :(

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

    Default

    Quote Originally Posted by dienesh77 View Post
    I dont know how to test it if its a number conveniently.
    Check the Character class.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How do you extract trace? Whatever you are reading with just call readline and don't do anything with it, then start the loop. You can also use a Boolean to test if it's the first item being scanned, this may be a bit too challenging for you.

  11. #11
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    Im getting this error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at ReadPayroll.readDate(ReadPayroll.java:35)
    at ReadPayroll.main(ReadPayroll.java:18)
    Last edited by dienesh77; 04-06-2011 at 05:25 AM.

  12. #12
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    I Parsed the first line and it still wont do what I want it to do :(

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Before getting the value of trace add a line which reads a line, but does nothing with it.

  14. #14
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    OMG Thank you Sunde887 !!! YOUR THE BEST!!!

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You as welcome, please mark your thread solved with the thread tools at the top of the page if you are done.

Similar Threads

  1. Bug Reading txt files
    By MHardeman25 in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:03 PM
  2. Reading .dat files in java
    By anilreddy76 in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2010, 01:00 PM
  3. Reading .bin files
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-22-2010, 04:39 PM
  4. java api for reading adobe frame maker files
    By venkatmatcha in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2008, 06:00 AM
  5. Does OS intervene when reading Java text files
    By Tina G in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2008, 02:29 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
  •