从文件扫描时出现Java InputMismatchException错误

时间:2017-10-08 23:32:14

标签: java file java.util.scanner

程序正在从.txt文件中读取。扫描程序抛出以下错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at project.pkg2.Project2.main(Project2.java:38)
Java returned: 1
BUILD FAILED (total time: 0 seconds)

这是从文件中读取所有内容的代码。我们有一个整数,它是特定专辑的曲目数。然后我们在每一行都有一个曲目编号,歌曲标题和艺术家。曲目编号,标题和艺术家只用逗号分隔。

1 个答案:

答案 0 :(得分:1)

默认情况下,FILE *fp; /* Get dataset offset within file */ file_id = H5Fopen(FNAME, H5F_ACC_RDONLY, H5P_DEFAULT); status = H5Fget_vfd_handle(file_id, H5P_DEFAULT, &fhandle); dataset_id = H5Dopen(file_id, "/dset", H5P_DEFAULT); offset = H5Dget_offset(dataset_id); /* Read through stdio */ fp = fopen(FNAME, "rb"); fseek(fp, offset, SEEK_SET); int x0[NX*NY]; fread(&x0, sizeof(int), NX*NY, fp); /* Get the file descriptor */ fd = *((int *)fhandle); /* Read through Posix */ int x1[NX*NY]; lseek(fd, offset, SEEK_SET); read(fd, x1, NX*NY*sizeof(int)); /* Read through mmap */ // page size-aligned offset for mmap pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1); length = NX * NY * sizeof(int); addr = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_PRIVATE, fd, pa_offset); // revert the align for correct access int * x2 = (int *) (addr + offset - pa_offset); Scanner分隔符,当您执行JavaWhiteSpace时,它会获得" 1,"并尝试将其解析为trackNumber = sc.nextInt();,这显然会失败。 此外,您在获取int后忘记收集结束字符。

这是你应该做的:

trackCount