对不起,如果有noob错误。我是noob。
package lab7fall2015;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Lab7{
public static String computeMessage(double inputGpa){
String feedback = "";
if(inputGpa >= 3.8){
feedback = "Excellent!";
return feedback;
}
else if(inputGpa >= 3.4 && inputGpa < 3.8){
feedback = "Very Good!";
return feedback;
}
else if(inputGpa >= 2.8 && inputGpa < 3.4){
feedback = "Okay";
return feedback;
}
else if(inputGpa < 2.8){
feedback = "see your advisor";
return feedback;
}
else{
feedback = "error";
return feedback;
}
}
public static void main(String[] args) throws FileNotFoundException{
String myReaderRef = null;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Lab 7 Josh Peel");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"TXT - text files", "txt");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("C:\\Java"));
int picked = chooser.showOpenDialog(null);
if (picked == JFileChooser.APPROVE_OPTION){ //user picked something
myReaderRef = chooser.getSelectedFile().getAbsolutePath();
}
else{
System.exit(0);
}
FileReader myReader = new FileReader(myReaderRef);
Scanner input = new Scanner(myReader);
String output = "";
double gpaSum = 0.00;
while(input.hasNextLine()){
String readLastName = input.next();
String readFirstName = input.next();
String readMiddleName = input.next();
Double readGpa = input.nextDouble();
Student aStudent = new Student(readLastName, readFirstName, readMiddleName, readGpa);
String message = computeMessage(aStudent.getGPA());
gpaSum += aStudent.getGPA();
output += aStudent + " " + message + "\n";
}
input.close();
String blank = " ";
Student actor = new Student(blank, blank, blank, 0.0);
double gpaAvg = gpaSum / actor.getStudentCount();
output += "\n\nInput file read: " + myReaderRef + "\n";
output += "That file contains data on " + actor.getStudentCount() + " students\n";
output += "Average of all GPA's in that file is: " + gpaAvg;
JOptionPane.showMessageDialog(null, output, "Lab 7 by Josh Peel",
JOptionPane.INFORMATION_MESSAGE);
}
}
class Name{
private String lastName, firstName, middleName;
public Name(String studentLastName, String studentFirstName, String studentMiddleName){
lastName = studentLastName;
firstName = studentFirstName;
middleName = studentMiddleName;
}
public String toString(){
String studentName = firstName + " " + middleName + " " + lastName + " ";
return studentName;
}
}
class Student{
private static Name name;
private double gpa = 0.00;
private int numberOfStudents = 0;
public Student(String newLastName, String newFirstName,
String newMiddleName, double newGpa){
name = new Name(newLastName, newFirstName, newMiddleName);
gpa = newGpa;
numberOfStudents++;
}
public double getGPA(){
return gpa;
}
public String toString(){
return name + "(" + gpa + ")";
}
public int getStudentCount(){
return numberOfStudents - 1;
}
}
返回运行时错误,内容如下:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at lab7fall2015.Lab7.main(Lab7.java:64)
输入文件读取:
Smith Bob D. 3.14
Jones Mary K. 3.92
Wilson Donald J. 2.91
Martin Mark P. 3.44
Miller Marsha H. 3.66
Dummy Iam A. 1.11
Brilliant Johnny B. 4.0
我在代码中没有错误消息,但我是新手使用扫描仪并从文本文档中分配变量。我使用JFilechooser选择我的文件后失败了。
出于调试目的,我添加了“System.out.print(readLastName)”并输出所有姓氏。所以它可以逐行完成整个文档......