Computer Science Canada reading a particular line |
Author: | unoho [ Wed Mar 03, 2010 7:57 pm ] | ||
Post subject: | reading a particular line | ||
Hi guys, this is a sample input file that my program is supposed to read. (also, i put it as attachment) 467343 23750.40 W 250.00 D 1200.00 W 75.00 I 120.74 W 375.00 D 580.00 I 245.50 W 400.00 W 600.00 D 450.50 W 35.65 where the first line is account number (467343) and opening balance($23750.40) then i am supposed to input W(withdrawals) and D(Deposits) or I(interest) and do some calculation. Problem is that once i read the first line of the file, i can't read from the second line. It gives me an NoSuchElementException error. Here is a sample code:
any help would be much appreciated sorry..my code is kinda messy |
Author: | DemonWasp [ Wed Mar 03, 2010 8:27 pm ] |
Post subject: | RE:reading a particular line |
Your StringTokenizer operates on the first inputLine. Each time you assign to inputLine at the top of your while loop, that's a different actual string, and the StringTokenizer is NOT updated. You need to use either the method in String that returns an array of tokens, or a StringTokenizer you update each time you update inputLine, or a Scanner that you update each inputLine. |
Author: | unoho [ Sat Mar 06, 2010 8:31 pm ] |
Post subject: | RE:reading a particular line |
umm... we haven't learned Scanner class in school..but i could sure look it up. but any advice on how to update the StringTokenizer? |
Author: | TheGuardian001 [ Sat Mar 06, 2010 8:41 pm ] |
Post subject: | Re: reading a particular line |
There's no built in method that I know of. Reinitializing may be your only option if you want to keep using StringTokenizer. |