2

So I have to read this file into a linked list, but when I run the code it says unknown source for every scanner.next(); Any idea on how to fix it?

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

public class LinkedMain 
{
    public static <bankacctinfo> void main(String args[]) throws IOException
    {
        File fil1 = new File("AcctList");
        Scanner scanner = new Scanner(fil1).useDelimiter("[,|\n/\r]+");

        LinkedList<BankAcctInfo>list=new LinkedList<BankAcctInfo>();

        String nameFirst;
        String nameLast;
        int pin;
        double balance;

        while(scanner.hasNext())
        {
            nameFirst = scanner.next();
            nameLast = scanner.next();
            pin = scanner.nextInt();
            balance = scanner.nextDouble();
            BankAcctInfo b1 = new BankAcctInfo(nameFirst, nameLast, pin, balance);
            list.add(b1);

        }
        scanner.close();

    }
ntalbs
  • 24,984
  • 7
  • 57
  • 76
wedemroyz
  • 21
  • 2

1 Answers1

0

According to this thread your stack trace is reporting an unknown source error because you are using the JRE and not the JDK. You should make sure you have installed the latest version of the JDK from Oracle's website and set Eclipse to use it. That will give you more informative stack traces.

That being said, your error probably has to do with using a Scanner with both next() and nextInt() methods like that. I've had trouble with that in the past because it handles newline characters strangely, as detailed in this thread. Essentially, nextInt() doesn't consume the newline character.

One of the suggestions there is what I do, which is to just use next() and then parse the lines that are supposed to be integers with Integer.valueOf(x).

So:

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

public class LinkedMain 
{
    public static <bankacctinfo> void main(String args[]) throws IOException
    {
        File fil1 = new File("AcctList");
        Scanner scanner = new Scanner(fil1).useDelimiter("[,|\n/\r]+");

        LinkedList<BankAcctInfo>list=new LinkedList<BankAcctInfo>();

        String nameFirst;
        String nameLast;
        int pin;
        double balance;

        while(scanner.hasNext())
        {
            nameFirst = scanner.next();
            nameLast = scanner.next();
            pin = Integer.valueOf(scanner.next());
            balance = Double.valueOf(scanner.nextDouble());
            BankAcctInfo b1 = new BankAcctInfo(nameFirst, nameLast, pin, balance);
            list.add(b1);

        }
        scanner.close();

    }

}

Community
  • 1
  • 1
Rob Wise
  • 4,560
  • 3
  • 21
  • 30