我们的任务是检索一个文件并对其进行分类并在另一个文件中显示它。姓,名,然后是年级。
由于错误
,我在获得循环时遇到了麻烦"java.util.NoSuchElementException"
这只发生在我改变当前存在的循环时。我也有显示结果的问题。我显示的结果都在一行中,我不能让这种情况发生。下面是我到目前为止的代码:
import java.util.*;
import java.util.StringTokenizer;
import java.io.*;
import javax.swing.*;
import java.text.DecimalFormat;
class Grade
{
public static void main(String [] args)throws IOException
{
//declaring
String line = "";
StringTokenizer st;
String delim = " tnr,-";
String token;
String firstname;
String lastname;
String grade;
String S69andbelow="Students with 69 or belown";
String S70to79 ="Students with 70 to 79n";
String S80to89= "Students with 80 to 89n";
String S90to100= "Students with 90 to 100n";
int gradeint;
double gradeavg = 0;
int count = 0;
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");
FileInputStream finput = new FileInputStream(inputFile);
FileOutputStream foutput = new FileOutputStream(outputFile);
FileReader reader = new FileReader(inputFile);
BufferedReader in = new BufferedReader(reader);
Scanner std = new Scanner(new File("input.txt"));
Scanner scanner = new Scanner(inputFile);
BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
Scanner scan = new Scanner(S69andbelow);
//reading linev
line = scanner.nextLine();
st = new StringTokenizer(line, delim);
//avoiding selected characters
try
{
while(st.hasMoreTokens())
{
firstname = st.nextToken(); //<----error started to happen
lastname = st.nextToken(); //here
grade = st.nextToken(); //and here
//storing tokens into their properties
gradeint = Integer.parseInt(grade);
//converting token to int
gradeavg = gradeavg + gradeint;
//calculating avg
count++;
//recording number of entries
if (gradeint <=69)
{
S69andbelow = S69andbelow + lastname + " "
+ firstname + " " + "t" + grade + "n";
} // saving data by grades
else if (gradeint >= 70 && gradeint <= 79)
{
S70to79 = S70to79 + lastname + " " + firstname
+ " " + "t" + grade + "n";
} // saving data by grades
else if (gradeint >= 80 && gradeint <=89)
{
S80to89 = S80to89 + lastname + " " + firstname
+ " " + "t" + grade + "n";
} // saving data by grades
else
{
S90to100 = S90to100 + lastname + " " + firstname
+ " " + "t" + grade + "n";
} // saving data by grades
}//end while
System.out.println(S69andbelow + "n" + S70to79 + "n"
+ S80to89 + "n" + S90to100);
//caterorizing the grades
gradeavg = gradeavg / count;
//calculating average
DecimalFormat df = new DecimalFormat("#0.00");
out.write("The average grade is: "
+ df.format(gradeavg));
System.out.println("The average grade is: "
+ df.format(gradeavg));
Writer output = null;
output = new BufferedWriter(new FileWriter(outputFile));
// scanner.nextLine(S69andbelow);
//output.write(S69andbelow + "n" + S70to79 + "n"
// + S80to89 + "n" + S90to100);
// output.close();
}
catch( Exception e )
{
System.out.println(e.toString() );
}
// Close the stream
try
{
if(std != null )
std.close( );
}
catch( Exception e )
{
System.out.println(e.toString());
}
}
}
问题已得到解答。最后的修饰如下:/ * * * * * * * * * * * * * * * * * * * 专业名称:年级姓名:Dennis Liang截止日期:11/3/31程序描述:编写一个程序哪个从文件中读取一个列表学生与他们的成绩。也显示姓,名,然后是年级。 * * * * * * * * * * * * * * * * * * * * /
进口java.util。;进口java.util.StringTokenizer;进口. io . ;进口javax.swing。*;进口java.text.DecimalFormat;
类等级{public static void main(String [] args)抛出IOException
{
//declaring
String delim = " tnr,-";
String token;
String firstName;
String lastName;
String grade;
String S69andbelow="Students with 69 or belown";
String S70to79 ="Students with 70 to 79n";
String S80to89= "Students with 80 to 89n";
String S90to100= "Students with 90 to 100n";
String all;
int gradeint;
double gradeavg = 0;
int count = 0;
int countTwo = 0;
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");
FileInputStream finput = new FileInputStream(inputFile);
FileOutputStream foutput = new FileOutputStream(outputFile);
FileReader reader = new FileReader(inputFile);
BufferedReader in = new BufferedReader(reader);
Scanner std = new Scanner(new File("input.txt"));
Scanner scanner = new Scanner(inputFile);
BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
Scanner scan = new Scanner(S69andbelow);
try
{
String line, newLine = (String)System.getProperty("line.separator");
//seperating lines
while((line = in.readLine()) != null){
//Will only work if the file is properly formatted
StringTokenizer st = new StringTokenizer(line, " ");
if (st.countTokens() == 3)
{
firstName = st.nextToken();
lastName = st.nextToken();
grade = st.nextToken();
gradeint = Integer.parseInt(grade);
//retrieving tokens
gradeavg = gradeavg + gradeint;
//formula for avg
count++;
//avg count
//organizing by grades
if (gradeint <=69)
{
S69andbelow = S69andbelow + newLine+ lastName + " "
+ firstName + " " + "t" + grade + newLine;
}
else if (gradeint >= 70 && gradeint <= 79)
{
S70to79 = S70to79 +newLine+ lastName + " " + firstName
+ " " + "t" + grade + newLine;
}
else if (gradeint >= 80 && gradeint <=89)
{
S80to89 = S80to89 + newLine+ lastName + " " + firstName
+ " " + "t" + grade + newLine;
}
else
{
S90to100 = S90to100 + newLine+ lastName + " " + firstName
+ " " + "t" + grade + newLine;
}
}其他{countTwo + +;system . out。println("There is"+ countTwo +"person/people"+"需要额外信息" + "n");}//通知用户有多少人}//信息缺失或过多。
System.out.println(S69andbelow + "n" + S70to79 + "n"
+ S80to89 + "n" + S90to100);
//caterorizing the grades
gradeavg = gradeavg / count;
//calculating average
DecimalFormat df = new DecimalFormat("#0.00");
//formating
System.out.println("The average grade is: "
+ df.format(gradeavg));
//displaying
Writer output = null;
output = new BufferedWriter(new FileWriter(outputFile));
//make file
output.write(newLine + S69andbelow + newLine + S70to79 + newLine
+ S80to89 + newLine + S90to100);
output.write("The average grade is: "
+ df.format(gradeavg));
//outputing to file
output.close();
//saving
}catch(异常e){
System.out.println (e.toString ());}//关闭流试一试{If (std != null)
}
catch( Exception e )
{
System.out.println(e.toString());
}
}}
您需要在每次调用st.nextToken()之前检查st.hasMoreTokens()
对于新行,windows要求n
或者,你可以使用System.getProperty("line.separator"),它会给你每个操作系统正确的换行符。
String line, newLine = (String)System.getProperty("line.separator");
while((line = in.readLine()) != null){
//Will only work if the file is properly formatted
StringTokenizer st = new StringTokenizer(line, " ");
firstName = st.nextToken();
lastName = st.nextToken();
grade = st.nextToken();
gradeint = Integer.parseInt(grade);
gradeavg = gradeavg + gradeint;
count++;
if (gradeint <=69)
{
S69andbelow = S69andbelow + lastname + " "
+ firstname + " " + "t" + grade + newLine;
}
else if (gradeint >= 70 && gradeint <= 79)
{
S70to79 = S70to79 + lastname + " " + firstname
+ " " + "t" + grade + "n";
}
else if (gradeint >= 80 && gradeint <=89)
{
S80to89 = S80to89 + lastname + " " + firstname
+ " " + "t" + grade + newLine;
}
else
{
S90to100 = S90to100 + lastname + " " + firstname
+ " " + "t" + grade + newLine;
}
}