Results 1 to 15 of 15
Thread: Reading files JAVA
- 04-06-2011, 03:56 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
- 04-06-2011, 03:59 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-06-2011, 04:07 AM #3
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.
- 04-06-2011, 04:09 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
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();
}
- 04-06-2011, 04:11 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
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)
- 04-06-2011, 04:13 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
Yes the first line is the date.
How can I ignore the first line, Im having trouble with that part.
- 04-06-2011, 04:15 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 04-06-2011, 04:26 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
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 :(
- 04-06-2011, 04:31 AM #9
- 04-06-2011, 04:40 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-06-2011, 05:02 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
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.
- 04-06-2011, 05:02 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
I Parsed the first line and it still wont do what I want it to do :(
- 04-06-2011, 05:06 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Before getting the value of trace add a line which reads a line, but does nothing with it.
- 04-06-2011, 05:19 AM #14
Member
- Join Date
- Feb 2011
- Posts
- 32
- Rep Power
- 0
OMG Thank you Sunde887 !!! YOUR THE BEST!!!
- 04-06-2011, 05:22 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Bug Reading txt files
By MHardeman25 in forum New To JavaReplies: 4Last Post: 08-13-2010, 10:03 PM -
Reading .dat files in java
By anilreddy76 in forum Advanced JavaReplies: 2Last Post: 07-23-2010, 01:00 PM -
Reading .bin files
By spatel14 in forum New To JavaReplies: 3Last Post: 06-22-2010, 04:39 PM -
java api for reading adobe frame maker files
By venkatmatcha in forum Advanced JavaReplies: 2Last Post: 06-30-2008, 06:00 AM -
Does OS intervene when reading Java text files
By Tina G in forum Advanced JavaReplies: 1Last Post: 04-07-2008, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks