for循环的目的是为文件中的每一行文本创建一个新的currentJob并将其存储在一个数组中。循环存储第一行文本,但不存储任何其他内容。
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)
{
}
}
}
`` 这就是我现在所得到的
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下至[1204,1205,0,0]是该行的内容。你可以看到它存储文本文件的第一行,然后什么也没有。我需要帮助来解决这个问题。
完全忘记了catch语句是空的但是它显示了一些问题
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)
以上是我得到的。
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
答案 0 :(得分:2)
你应该把
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
在你的for循环中。
答案 1 :(得分:1)
您提到此处发生异常NoSuchElementException
:
int jID = lineScanner.nextInt();
这意味着,扫描程序会尝试读取不存在的int。主要问题是您当前的代码结构:
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++;
}
您正在阅读一些内容并将其分配给变量,然后您将一直读到最后一个整数(lineScanner.hasNextInt()
)。如果没有可读的整数可用,则此循环将停止。在那之后,你在做更多不需要的东西。
这是for (int i = 0; i < jobArray.length; i++)
的第一次迭代。现在是下一次迭代。 lineScanner
仍然相同,因此由于循环while(lineScanner.hasNextInt())
而有读取整数,但第二次迭代将到达行int jID = lineScanner.nextInt();
。扫描程序尝试读取int,找不到要读取的内容并抛出NoSuchElementException
。
要解决此问题,您应重置lineScanner
以阅读新行。
或者删除for
循环,因为while(scanner.hasNextLine())
将负责阅读每个可用行,并且lineScanner
将正确填充。请重新考虑变量Job[] jobArray = new Job[30];
的当前位置。您将丢失文件每个新行的数据。