gpt4 book ai didi

StringTokenizer 中出现错误的 java 项目

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:21 25 4
gpt4 key购买 nike

我制作了一个涉及 StringTokenizer 的 java 项目,我使用了文件 FemaleCoursesElec.csv,该文件用逗号分隔,然后我用它来填充 array1 但有一个问题我不知道它是什么?

这是类(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;






}
}

主程序在这里:

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]);

}

}

但是每次我运行这个程序时,StringTekonizer 都会出现错误?

故障显示如下

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)

我尝试了很多次来解决这个问题,但都无法解决,我该如何解决这个问题?

最佳答案

这里:

        while(token.hasMoreTokens()) {

保证至少拥有一个 token 。但是,在这里:

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

您迭代了多个。每个 hasMoreTokens() 只能调用一次 nextToken()

关于StringTokenizer 中出现错误的 java 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com