Java 接收项目的NoSuchElementException
我正在开发一个程序,该程序旨在从文件中获取数据,并在满足某些条件时将其写入新文件。但是,运行我的代码会在第41行给出一个NoSuchElementException,第一个令牌应该在该行生成(name=st.nextToken();)。我不完全清楚为什么会发生这种情况。我是否正确使用了stringTokenizer,还是我的循环有问题 以下是完整的代码:Java 接收项目的NoSuchElementException,java,Java,我正在开发一个程序,该程序旨在从文件中获取数据,并在满足某些条件时将其写入新文件。但是,运行我的代码会在第41行给出一个NoSuchElementException,第一个令牌应该在该行生成(name=st.nextToken();)。我不完全清楚为什么会发生这种情况。我是否正确使用了stringTokenizer,还是我的循环有问题 以下是完整的代码: import java.util.*; import java.io.*; public class Warning1 { public s
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");
}
}
}
import java.util.*;
导入java.io.*;
公开课警告1
{
公共静态void main(字符串[]args)引发IOException
{
int creditHrs,n=0;//获得的学期时数
双质量分数,gpa;//获得的质量分数和分数
平均分(质量分)
字符串名称=,inputName=“students.dat”,outputName=
“警告.dat”;
试一试{
文件文件=新文件(inputName);
扫描仪扫描=新扫描仪(文件);
FileWriter fw=新的FileWriter(outputName);
PrintWriter pw=新的PrintWriter(fw);
String[]fileLines=新字符串[100];//创建
串
while(scan.hasNext())
{
fileLines[n]=scan.nextLine();
n++;
}
int m=n;
scan.close();
for(int i=0;i
发布文件或部分them@EFliegel我建议。。查看记事本或等效文件中的文件students.dat
。@EFliegel可能会帮助您使用System.out.println
而不是pw.println
,这可能有助于了解students.dat
中的违规行。我刚刚发现了错误!当我将项目从学校电脑转移到笔记本电脑时,students.dat文件的格式更改为增加两倍间距,这使程序混乱。谢谢大家的帮助!发布文件或部分them@EFliegel我建议。。查看记事本或等效文件中的文件students.dat
。@EFliegel可能会帮助您使用System.out.println
而不是pw.println
,这可能有助于了解students.dat
中的违规行。我刚刚发现了错误!当我将项目从学校电脑转移到笔记本电脑时,students.dat文件的格式更改为增加两倍间距,这使程序混乱。谢谢大家的帮助!