문제

This is my first time posting here. I've lurked quite a bit in the past, but can't seem to figure out what my problem is this time. This is for homework, and I have tried to solve it myself.

public class Driver {

public static void main(String[] args) throws FileNotFoundException {

        String name = "", date = "", time = "";
        String month = "", day = "", year = "";

        ArrayList<Customer> customerArray = new ArrayList<Customer>();
        ArrayList<Date> dateArray = new ArrayList<Date>();

        File myFile = new File("./src/Program4/Customers.csv");

        Scanner fileScan = new Scanner(myFile);

        while (fileScan.hasNext()) {
            String lineString = fileScan.nextLine();

            Scanner lineScan = new Scanner(lineString);
            lineScan.useDelimiter(",");

            while (lineScan.hasNext()) {

//              if (lineScan.next().equalsIgnoreCase("Name") == true
//                      || lineScan.next().equalsIgnoreCase("Date") == true
//                      || lineScan.next().equalsIgnoreCase("Time") == true) {                  
//                  lineScan.next();                    
//              } 
            //  else {      

                    name = lineScan.next();
                    date = lineScan.next();
                    time = lineScan.next();                     

                    Scanner dateScan = new Scanner(date);
                    dateScan.useDelimiter("/");

                    while (dateScan.hasNext()) {
                        month = dateScan.next();
                        day = dateScan.next();
                        year = dateScan.next();
                    }

                    Customer newCustomer = new Customer(name, date, time, day,
                            month);
                    customerArray.add(newCustomer);
                //}
            }
        } } }

The commented out part is just me seeing if that was the problem, as I was afraid the reason it wasn't working was because the header didn't have any "/". However, that wasn't the case. Here's a sample of the file I'm reading in:

Name    Date    Time
SK]CD[TUI   12/3/2014   5:41 AM
KXFPPHXPK   12/9/2014   7:43 AM
RVZTA[KLT   7/17/2014   9:35 AM
AUTVFXVZW   12/27/2014  2:39 AM
CFH\XBJRE   2/3/2014    9:51 AM
RT@KNCPHO   4/19/2014   8:42 AM

I have to sort them by date first, then time. I was using insertion sort but it was only reading the first digit of the date, it was ignoring the double digits. So, my solution was to have another scanner that broke up the date, then sort by that. I didn't get too far into that, though, because I'm getting the following error.

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at Program4.Driver.main(Driver.java:55)

Line 55 being:

day = dateScan.next();

So, no clue what the problem is. Hopefully my method is the best way to sort this, if it's not, it'd be nice if someone could save me some time and suggest an alternative.

Thanks!

====================

Edit:

Well, I figured out the initial problem. I added this:

    if (lineScan.next().equalsIgnoreCase("Name") == true) {
        lineScan.nextLine(); } 
else {

    name = lineScan.next();
    date = lineScan.next();
    time = lineScan.next();

That somewhat fixed the problem, but I can't figure out why the scanner is skipping the first part of the file, the "name." "name" becomes the date, "date" becomes time, and "time" has nothing to grab so it throws no such element. I'm doing nextLine, so it shouldn't be eating the \n.

도움이 되었습니까?

해결책

Homework hint:

You need to use your IDE's debugger to solve this. Set a breakpoint at this line:

    Scanner dateScan = new Scanner(date);

and see what date is. If that doesn't make the problem obvious to you, then use the debugger to execute the statements one at a time, watching what the next calls are returning. (You may need to go around the outer loop a couple of times ...)


P.S. I think I can see what the problem is ... but if you follow my advice, you should be able to find it yourself quickly. And learn to use the debugger :-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top