简体   繁体   中英

for loop filling array[0] but thats it

The aim of the for loop is to create a new currentJob for each line of text from the file and store it in an array. The loop stores the first line of text but doesn't store anything else.

public static void job()
{
    File file = new File("DailyJobs.txt");

    try
    {
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            Scanner lineScanner = new Scanner(line);
            lineScanner.useDelimiter(",");

            Job[] jobArray = new Job[30];

            for (int i = 0; i < jobArray.length; i++)
            {
                Job currentJob = new Job();

                int jID = lineScanner.nextInt();
                int cID = lineScanner.nextInt();
                String rego = lineScanner.next();
                String date = lineScanner.next();
                String day = lineScanner.next();
                double fee = lineScanner.nextDouble();

                int[] sc = new int[4];
                int f = 0;
                while(lineScanner.hasNextInt())
                {
                    lineScanner.useDelimiter(",");
                    sc[f] = (lineScanner.nextInt());
                    f++;
                }

                currentJob.setJobID(jID);
                currentJob.setCustomerID(cID);
                currentJob.setRegistration(rego);
                currentJob.setDate(date);
                currentJob.setDay(day);
                currentJob.setTotalFee(fee);
                currentJob.setServiceCode(sc);

                jobArray[i] = currentJob;


                System.out.println(currentJob.getJobID());
                System.out.println(currentJob.getCustomerID());
                System.out.println(currentJob.getRegistration());
                System.out.println(currentJob.getDate());
                System.out.println(currentJob.getDay());
                System.out.println(currentJob.getTotalFee());
                System.out.println(Arrays.toString(currentJob.getServiceCode()));

                System.out.println(Arrays.toString(jobArray));



            }


        }
    }catch (Exception e)
    {

    }
}
}

`` This what I'm getting at the moment

90301
14304
ITGURU
11/11/2014
Tuesday
735.75
[1204, 1205, 0, 0]
[Job@1c31e2ad, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

90301 down to [1204, 1205, 0, 0] is the contents of the line. As you can see it's storing the first line of the text file and then nothing. I need help to fix this.

Completely forgot about the catch statement being empty but yes it's showing some issues

java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at test3.job(test3.java:31)
    at test3.main(test3.java:9)

The above is what I'm getting.

DailyJobs.txt

90301,14304,ITGURU,11/11/2014,Tuesday,735.75,1204,1205
90302,14314,ROK5TR,12/11/2014,Wednesday,335.75,1205
90303,14318,HRTBRK,13/11/2014,Thursday,125.5,1200
90304,14310,EVOH8R,14/11/2014,Friday,1207.95,1211
90305,14300,G4MER,11/11/2014,Tuesday,500,1204,1202
90306,14312,COFFEE,11/11/2014,Tuesday,100,1202
90307,14316,POPULR,11/11/2014,Tuesday,150,1203
90308,14317,WRXSTI,11/11/2014,Tuesday,1307.95,1211,1202
90309,14321,FAST,11/11/2014,Tuesday,400,1204
90310,14308,PRQUL8,14/11/2014,Friday,150,1203
90311,14315,OK2BL8,14/11/2014,Friday,335.75,1205
90312,14320,OLDGUY,11/11/2014,Tuesday,1207.95,1211
90313,14313,ABC123,12/11/2014,Wednesday,,1202,1203,1205,1210
90314,14306,WAIT,11/11/2014,Tuesday,150,1203
90315,14302,FUZZ,14/11/2014,Friday,335.75,1205
90316,14313,GO,12/11/2014,Wednesday,,1204,1202
90317,14309,SING,13/11/2014,Thursday,400,1204
90318,14307,MANIC,12/11/2014,Wednesday,100,1202
90319,14305,URABUS,14/11/2014,Friday,1207.95,1211
90320,14303,STOP,14/11/2014,Friday,150,1203
90321,14310,BAKOFF,11/11/2014,Tuesday,,1204,1205
90322,14322,WINNER,14/11/2014,Friday,1207.95,1211
90323,14324,KITTY,13/11/2014,Thursday,400,1204
90324,14325,WEDDIN,12/11/2014,Wednesday,100,1202
90325,14326,FIGHTR,11/11/2014,Tuesday,100,1202
90326,14320,OLDGUY,14/11/2014,Friday,175.9,1208
90327,14322,QUICK,12/11/2014,Wednesday,,1203,1204,1209
90328,14322,L8R,13/11/2014,Thursday,400,1204
90329,14299,TRUTH,13/11/2014,Thursday,80.25,1197
90330,14337,NIGHT,13/11/2014,Thursday,150,1203
90331,14312,COFFEE,13/11/2014,Thursday,350,1204

You should put

        String line = scanner.nextLine();
        Scanner lineScanner = new Scanner(line);
        lineScanner.useDelimiter(",");

in your for loop.

You mentioned that the exception NoSuchElementException occures here:

int jID = lineScanner.nextInt();

This means, the scanner tries to read an int, that is not there. The main problem is your current code structure:

int jID = lineScanner.nextInt();
int cID = lineScanner.nextInt();
String rego = lineScanner.next();
String date = lineScanner.next();
String day = lineScanner.next();
double fee = lineScanner.nextDouble();

// omitted code
while(lineScanner.hasNextInt())
{
    lineScanner.useDelimiter(",");
    sc[f] = (lineScanner.nextInt());
    f++;
}

You're reading some stuff and assigning it to variables, then you're reading until the last integer ( lineScanner.hasNextInt() ). This loop will stop if there is no more integer to read available. After that, you're doing more stuff that is not necessary here.

This was the first iteration of for (int i = 0; i < jobArray.length; i++) . Now comes the next iteration. The lineScanner is still the same, therefore there is integer to read due to the loop while(lineScanner.hasNextInt()) , but the second iteration will reach the line int jID = lineScanner.nextInt(); . The scanner tries to read an int, can't find something to read and throws an NoSuchElementException .

To fix that, you should reset your lineScanner to read a new line. Or delete the for loop, because the while(scanner.hasNextLine()) will take care of reading each available line and your lineScanner will be "filled" correctly. And please re-think the current position of your variable Job[] jobArray = new Job[30]; . You'll lost his data on each new line of your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM