简体   繁体   English

java.net项目中StringTokenizer中的错误

[英]java Project with error in StringTokenizer

I made a java project that involves a StringTokenizer, I used the file FemaleCoursesElec.csv, that which is separated by a comma, then I used it to fill the array1 but there is a problem I don't know what its? 我制作了一个涉及StringTokenizer的Java项目,我使用了文件FemaleCoursesElec.csv,该文件用逗号分隔,然后用它填充array1,但是有一个问题,我不知道它是什么?

this was the class : 这是课程:

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package shakeelprojectmain; /** * * @author lenovo */ import java.util.*; import java.io.*; public class ArrayOfCourses { public String[][] getAllCourses() throws IOException { String[][] array1 = new String[getRow()][getCol()]; int i = 0; String str; File file = new File("FemaleCoursesElec.csv"); Scanner in = new Scanner(file); while(in.hasNext()) { str = in.nextLine(); StringTokenizer token = new StringTokenizer(str,","); while(token.hasMoreTokens()) { for (int j = 0 ; j < getCol() ; j++) { array1[i][j] = token.nextToken(); } } i++; } in.close(); return array1; } /** * * @return * @throws java.io.IOException */ public int getRows() throws IOException { int row = 0; File file = new File("FemaleCoursesElec.csv"); Scanner in = new Scanner(file); while(in.hasNext()) { in.nextLine(); row++; } in.close(); return row; } public int getCol() throws IOException { int col = 0; File file = new File("FemaleCoursesElec.csv"); Scanner in = new Scanner(file); String str = in.nextLine(); StringTokenizer token; token = new StringTokenizer(str,"," ); while(token.hasMoreTokens()) { token.nextToken(); col++; } in.close(); return col; } private int getRow() throws IOException { int row = 0; File file = new File("FemaleCoursesElec.csv"); Scanner inputFile = new Scanner(file); while(inputFile.hasNext()) { inputFile.nextLine(); row++; } inputFile.close(); return row; } } 

the Main Program is here : 主程序在这里:

 package shakeelprojectmain; import java.io.*; import java.util.*; /** * * @author lenovo */ public class ShakeelProjectMain { /** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String[] args) throws IOException { ArrayOfCourses array11 = new ArrayOfCourses(); String[][] array1 = array11.getAllCourses(); System.out.println(array1[0][0]+"\\n" + array1[0][2]); } } 

but every time I run this it appears there is a fault in StringTekonizer? 但是每次运行此命令时,StringTekonizer似乎都出现了故障?

the fault shows this 错误显示了这一点

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at shakeelprojectmain.ArrayOfCourses.getAllCourses(ArrayOfCourses.java:40)
at shakeelprojectmain.ShakeelProjectMain.main(ShakeelProjectMain.java:25)

I tried many times to solve this problem but I couldn't, how should I solve this? 我尝试了很多次来解决这个问题,但是我不能,应该如何解决呢?

Here: 这里:

        while(token.hasMoreTokens()) {

It is guaranteed to have at least one token. 保证至少有一个令牌。 But, here: 但在这儿:

            for (int j = 0 ; j < getCol() ; j++) {
                array1[i][j] = token.nextToken();

you iterate through more than one. 您遍历不止一个。 You can only have one nextToken() call per hasMoreTokens() . 每个hasMoreTokens()只能调用一次nextToken() hasMoreTokens()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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