Our assignment is to retrieve a file and categorize it and display it in another file. Last name first name, then grade.
I am having trouble with getting a loop going because of the error
"java.util.NoSuchElementException"
This only happens when I change the currently existing while I loop I have. I also have a problem of displaying the result. The result I display is all in one line, which I can't let happen. Here is my code so far:
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 = " \t\n\r,-";
String token;
String firstname;
String lastname;
String grade;
String S69andbelow="Students with 69 or below\n";
String S70to79 ="Students with 70 to 79\n";
String S80to89= "Students with 80 to 89\n";
String S90to100= "Students with 90 to 100\n";
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());
}
}
}
Question has been answered. The final touches is as presented: /******************* Program Name: Grade Name: Dennis Liang Due Date: 3/31/11 Program Description: Write a program which reads from a file a list of students with their Grade. Also display last name, first name, then grade. ********************/
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 delim = " \t\n\r,-";
String token;
String firstName;
String lastName;
String grade;
String S69andbelow="Students with 69 or below\n";
String S70to79 ="Students with 70 to 79\n";
String S80to89= "Students with 80 to 89\n";
String S90to100= "Students with 90 to 100\n";
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;
}
} else{ countTwo ++; System.out.println("There is "+ countTwo + " person/people" + " that require additional information" + "\n"); } //informing the user that there a how many people with } //missing information or too much.
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( Exception e ) {
System.out.println(e.toString() ); } // Close the stream try { if(std != null ) std.close( ); }
catch( Exception e )
{
System.out.println(e.toString());
}
} }
You need to check st.hasMoreTokens() before each time you call st.nextToken()
As far as the new line, windows requires \n\0
Alternatively, you can use System.getProperty("line.separator") which gives you the correct newline for each OS.
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;
}
}
精彩评论