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:
code:

package hossaina_assn02;
import java.io.*;
import java.text.DecimalFormat;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws FileNotFoundException, IOException{
        int accountNumber, countDeposit=0, countWithdraw=0;
        double openingBalance=0, transValue=0, newBalance=0;
        double interest=0, deposit=0, withdraw=0;
        char transType;
        boolean isServiceCharged = false;

        BufferedReader inData = new BufferedReader(new FileReader("account.txt"));
        PrintWriter outData;
        String inputLine = inData.readLine();
        StringTokenizer tokens = new StringTokenizer(inputLine);
        DecimalFormat decFormat = new DecimalFormat("0.00");

        accountNumber = Integer.parseInt(tokens.nextToken());
        openingBalance = Double.parseDouble(tokens.nextToken());

        while((inputLine = inData.readLine())!=null){
            transType = tokens.nextToken().charAt(0);
            System.out.println(transType);
            transValue = Double.parseDouble(tokens.nextToken());
            switch (transType){
                case 'w':
                case 'W':
                    withdraw += transValue;
                    countWithdraw++;
                    break;
                case 'd':
                case 'D':
                    deposit += transValue;
                    countDeposit++;
                case 'i':
                case 'I':
                    interest += transValue;
                    break;
            }
            newBalance = openingBalance + deposit + interest - withdraw;
            if (newBalance <1000 && isServiceCharged == false){
                isServiceCharged = true;
                newBalance -= 25;
            }

        }

        inData.close();
        System.out.println(newBalance);
}
}


any help would be much appreciated Smile
sorry..my code is kinda messy Neutral

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.


: