简体   繁体   中英

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?

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?

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() .

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