开发者

Reading file data into LinkedList error

I am working on a code that reads data about customers from an input file and stores them into a linkedlist of objects of customer. the linked list implementation is not the JVM one. while reading the data using the readFile(), it's giving me a NumberFormatException: For input string: "Ben Affleck" error. here's the method. the basic idea of the logic is to read the first record initially and set it as the head of the linked list and then read the subsequent records. the error occurs during the if conditional when it checks for duplicate account id's. the way i coded it was if the id's match then skip those many number of lines to the next record. the Acd() method enters items in ascending order in the linkedlist. help would be greatly appreciated. kindly let me know if the question is unclear.

public static int readFile(String filename, LinkedList<Customer> review) throws IOException{
    Scanner scan = new Scanner (new File (filename));
    /*Reading the first record separatly*/
    Customer head = new Customer();
    Node<Customer> first = new Node<Customer>(head);

    String[] a = scan.nextLine().split("=");
    int  accId = Integer.parseInt(a[1].trim());
    a = scan.nextLine().split("=");
    String name = a[1].toUpperCase().trim();
    a = scan.nextLine().split("=");
    String address =a[1].trim();
    a = scan.nextLine().split("=");
    String phone_number =(a[1].trim());
    a = scan.nextLine().split("=");
    String date_of_birth =(a[1].trim());
    a = scan.nextLine().split("=");
    double balance =(Double.parseDouble(a[1].trim()));
    a= scan.nextLine().split("=");
    String accType =(a[1].trim());

    if (accType.equals("Saving")){
        Customer temp = new Account1();
        Node<Customer> firstItem = new Node<Customer>(temp);
        first = firstItem;
    }

    else if(accType.equals("Checking")){
        Customer temp = new Account2();
        Node<Customer> firstItem = new Node<Customer>(temp);
        first = firstItem;
    }

    else if(accType.equals("Fixed")){
        Customer temp = new Account3();
        Node<Customer> firstItem = new Node<Customer>(temp);
        first = firstItem;
        a = scan.nextLine().split("=");
        ((Account3)first.item).set_intRate(Double.parseDouble(a[1].trim()));
    }

    first.item.set_account_id(accId);
    first.item.set_name(name);
    first.item.set_address(address);
    first.item.set_phone_number(phone_number);
    first.item.set_date_of_birth(date_of_birth);
    first.item.set_balance(balance);

    review.head= first;
    count = count+1;
    scan.nextLine();// resets the buffer reader

    while (scan.hasNext()&& count>0){
        Customer item = new Customer();
        Node<Customer> temp = new Node<Customer>(item);

        String[] st = scan.nextLine().split("=");
        Customer ctr = new Customer();
        Node<Customer> counter = new Node<Customer>(ctr);
        counter=review.head; // counter pointing to head
        int i=0;

        while(counter!=null){
            if(Integer.parseInt(st[1].trim())== review.getItem(i).get_accountid()){ // checking for duplicate records
                System.out.println("This account id is already in use so the record won't be read");

                while(!scan.nextLine().equals(" "))
                    scan.nextLine();
                scan.nextLine(); //to bring the reader back to the accoutnId
            }
            else
                break;


            int AccId = Integer.parseInt(st[1].trim());
            st = scan.nextLine().split("=");
            String AccName = st[1].toUpperCase().trim();
            st = scan.nextLine().split("=");
            String AccAdd =st[1].trim();
            st = scan.nextLine().split("=");
            String AccPhNum =(st[1].trim());
            st = scan.nextLine().split("=");
            String AccDob =(st[1].trim());
            st = scan.nextLine().split("=");
            double AccBal =(Double.parseDouble(st[1].trim()));
            st= scan.nextLine().split("=");
            String AccType =(st[1].trim());

            if (AccType.equals("Saving")){
                Customer a1 = new Account1();
                Node<Customer>Item = new Node<Customer>(a1);
                temp =  Item;
            } else if(AccType.equals("Checking")){
                Customer a2 = new Account2();
                Node<Customer>Item = new Node<Customer>(a2);
                temp =  Item;
            } else if(AccType.equals("Fixed")){
    开发者_如何学Go            Customer a3 = new Account3();
                Node<Customer>Item = new Node<Customer>(a3);
                temp =  Item;
                st = scan.nextLine().split("=");
                ((Account3)temp.item).set_intRate(Double.parseDouble(a[1].trim()));
             }

             temp.item.set_account_id(AccId);
             temp.item.set_name(AccName);
             temp.item.set_address(AccAdd);
             temp.item.set_phone_number(AccPhNum);
             temp.item.set_date_of_birth(AccDob);
             temp.item.set_balance(AccBal);

             if (scan.hasNextLine()){
                scan.nextLine();
             }


             review.insertAcd(temp.item);
             count= count+1;
             counter=counter.next;
         }

        if (count>=30){
            System.out.println("The number of records read has exceeded the limit and it will stop reading now");
            break;
        }

    }

    return count;
}

The input file is:

Account Id = 123
Name = Matt Damon
Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
DOB = 10-10-1970
Phone Number = 790-3233
Account Balance = 405600.00
Account Type = Fixed
Fixed Daily Interest = 0.05

Account Id = 126
Name = Ben Affleck
Address = 200 Hunting Street, Singapore 784563
DOB = 25-10-1968
Phone Number = 432-4579
Account Balance = 530045.00
Account Type = Saving

Account Id = 65
Name = Salma Hayek
Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
DOB = 06-04-73
Phone Number = 790-0000
Account Balance = 2345.00
Account Type = Checking

Account Id = 78
Name = Phua Chu Kang
Address = 50 PCK Avenue, Singapore 639798
DOB = 11-08-64
Phone Number = 345-6780
Account Balance = 0.00
Account Type = Checking

Account Id = 234
Name = Zoe Tay
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00
Account Type = Saving

Account Id = 2350
Name = Zoe Tay
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00
Account Type = Fixed
Fixed Daily Interest = 0.055


The first record has more lines (it has a "Fixed Daily Interest") than the second, so you may think you are reading in a String but it is actually a Double (or vice versa). So you will need to modify your code to either take into consideration this extra line or remove it from the first record as your code is expecting int, String, String, String, String, double, String whereas the first record is int, String, String, String, String, double, String, double.

This is not really the optimum solution to this problem, as you are repeating a chunk of code. It really could be in a single loop I think. It is definitely a type conversion problem like I initially said. You are attempting to get an integer out of a String that does not contain a number. Java is correctly telling you that there is no parsable Integer.

I will try and compile your code and see if I can pinpoint the exact error but what I have written above should give you enough of an idea to find out where the breakage is. Basically you think you are reading one line of your input file whereas you are actually on the line above or below.

Edit: Well I've hacked up your code and got it to compile. From an initial inspection it looks like that Matt Damon is OK but it is the second loop that is incorrect. You have an code that looks like this:

while (scan.hasNext()&& count>0){
    Customer item = new Customer();
    Node<Customer> temp = new Node<Customer>(item);
    String[] st = scan.nextLine().split("=");

    ....

    while(counter!=null){
        if(Integer.parseInt(st[1].trim())== review.getItem(i).get_accountid()){
            ...
        } else {
            break;
        }
    }
}

The account number st[1].trim() (this is 126 from your input file by the way) does not match since Matt Damon is the only one so far, so the code breaks out of the inner while condition and then proceeds to read the next line - "Ben Affleck". Then it enters the inner while loop again and tried to do Integer.parseInt on "Ben Affleck" which as you see is a NumberFormatException.

Edit 2:

Having looked over your other questions it looks like you are getting the SO community to write a lot of the application for you! It is clear you are learning Java but this may not be the best way to learn Java in my opinion! Don't worry though, we've all been there :-)

Without stepping through your exact code I cannot really answer the question exactly. Note that it cannot be compiled standalone the form given above since it is missing dependent classes, a main() and import statements.

So my answer is going to be mostly pseudocode for your entire readFile function since I see no reason why the first record should be read in separately and I think the function is overly complex for what it needs to do.

Scanner scan = new Scanner (new File (filename));

// maintain collecction of Account Number <-> Account details
Map<Integer, Customer> accounts = new HashMap<Integer, Customer>();

String[] aLine = null;

while (scan.hasNext()) {
    // read all of one account details
    aLine= scan.nextLine().split("=");
    int accId = Integer.parseInt(aLine[1].trim());
    aLine= scan.nextLine().split("=");
    String name = aLine[1].toUpperCase().trim();
    etc...

    String accType =(a[1].trim());

    if (accType.equals("Saving")) {
        ...
    } else {
        ...
    }

    // create Integer version of the accId to use as the key (the lookup)
    // into the collection of details
    Integer key = new Integer(accId);

    if (accounts.containsKey(key)) {
        // already added to the collection so 
        // no need to create a new Customer
    } else {
        // create new Customer
        Customer c = new Customer();
        c.set_account_id(accId);
        etc...

        // and add to the collection
        c.put(key, c);
    }

    // skip over blank lines
    while(!scan.nextLine().equals(" ")) {
        scan.nextLine();
    }
}

You may want to add some constraints to the while condition to limit the number of accounts added (as you have that in your existing code). For example:

while (scan.hasNext() && accounts.size() < 30) {

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜