接收Project的NoSuchElementException

时间:2017-09-06 01:41:57

标签: java

我正在开发一个程序,该程序用于从文件中获取数据,并在满足特定条件时将其写入新文件。但是,运行我的代码会在第41行给出一个NoSuchElementException,其中应该创建第一个标记(name = st.nextToken();)。我不完全确定为什么会这样。我正确使用stringTokenizer,还是我的循环问题?

以下是完整代码:

import java.util.*;
import java.io.*;

public class Warning1
{
public static void main (String[] args) throws IOException
{

int creditHrs, n = 0;     // number of semester hours earned
double qualityPts, gpa; // number of quality points earned and grade 
point (quality point) average
    String name = "", inputName = "students.dat", outputName = 
"warning.dat";
    try{
    File file = new File(inputName);
    Scanner scan = new Scanner(file);

    FileWriter fw = new FileWriter(outputName);
    PrintWriter pw = new PrintWriter(fw);

    String[] fileLines = new String[100]; //Create an Array of 
Strings

    while(scan.hasNext())
    {
        fileLines[n] = scan.nextLine();
        n++;
    }
    int m = n;
    scan.close();
    for(int i = 0; i < m; i++)
    {
        StringTokenizer st = new StringTokenizer(fileLines[i], " "); 
//Declare a StringTokenizer
        name = st.nextToken();
        creditHrs = Integer.parseInt(st.nextToken());
        qualityPts = Double.parseDouble(st.nextToken());
        gpa = qualityPts / (double) creditHrs;
        if ((gpa < 1.5 && creditHrs < 30) || (gpa < 1.75 && creditHrs 
< 60))
        {
            pw.println(name + " " + creditHrs + " " + gpa);
        }
    }
    pw.close();
    }
    catch (FileNotFoundException exception)
    {
        System.out.println("The requested file is not located in the 
package. Please move it to it's destination.");
    }
    catch (NumberFormatException exception)
    {
        System.out.println("The program could not parse the data 
properly. Make sure each line in the file contains a string, integer, 
and double, in that order");
    }
    catch (IOException exception)
    {
        System.out.println("Unexpected error. Please check your source file and try again");
    }
}
}

0 个答案:

没有答案