Java 如何修复“解析时到达文件末尾”
我正在编写一个程序来查找和显示具有4个属性(名字、姓氏、年龄、GPA)的班级中GPA最高的学生以及GPA最低的学生 我的代码的输出导致构建成功,但是仍然会出现解析错误,并且没有正确的输出信息Java 如何修复“解析时到达文件末尾”,java,arrays,loops,class,Java,Arrays,Loops,Class,我正在编写一个程序来查找和显示具有4个属性(名字、姓氏、年龄、GPA)的班级中GPA最高的学生以及GPA最低的学生 我的代码的输出导致构建成功,但是仍然会出现解析错误,并且没有正确的输出信息 public class app { public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student
public class app
{
public static void main(String args[ ])
{
student st1 = new student("Rebecca", "Collins", 22, 3.3);
student st2 = new student("Alex", "White", 19, 2.8);
student st3 = new student("Jordan", "Anderson", 22, 3.1);
student[ ] studentArray = new student[3];
studentArray[0] = st1;
studentArray[1] = st2;
studentArray[2] = st3;
var maxStudent = studentArray[0];
// Start at 1 because we assumed the first student in the array
// has the current max.
//
for (int i = 1; i < studentArray.length; i++)
{
// If the current student has a GPA higher than the student
// with the current max, make the current student the student
// with the current max.
//
if(studentArray[i].gpa > maxStudent.getGpa())
{
boolean max = false;
boolean min;
min = false;
for (student studentArray1 : studentArray) {
boolean gpa = false;
}
System.out.print("The highest GPA is: "+max);
System.out.println();
System.out.print("The lowest GPA is: "+min);
System.out.println();
System.out.println("Name: "+ studentArray[i].firstName + " "+ studentArray[i].lastName);
System.out.println("Age: "+ studentArray[i].age);
System.out.println("GPA: "+ studentArray[i].gpa);
}
}
public class student
{
//class variables
public String firstName;
public String lastName;
public int age;
public double gpa;
public int max = 0;
public int min = 0;
//constructor method
student(String a, String b, int c, double d)
{
firstName = a;
lastName = b;
age = c;
gpa = d;
}
student(String a, String b, int c, double d, int e, int f)
{
firstName = a;
lastName = b;
age = c;
gpa = d;
min = e;
max = f;
}
//a method that returns the student's complete name
String getInfo()
{
return getFirstName() +" " + getLastName() +" " + getMax();
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String fn)
{
firstName = fn;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String ln)
{
lastName = ln;
}
public int getAge()
{
return age;
}
public void setAge(int x)
{
age = x;
}
public double getGpa()
{
return gpa;
}
public void getGpa(double g)
{
gpa = g;
}
public int getMax()
{
return max;
}
public void getMax(int e)
{
max = e;
}
public int getMin()
{
return min;
}
public void getMin(int f)
{
min = f;
}
}
我将非常感谢任何能够解决错误的见解和解决方案,以使此代码正常工作。您发布的代码可能存在许多问题;你似乎在使用一个内部学生班级;大括号不匹配,缩进似乎不一致,在10之前的Java版本中不存在var关键字,我们不知道您安装的JDK或配置的项目编译器级别。你的学生不应该有单独的最小和最大字段 关于一个拥有所有公共领域的班级的优点有一些争论;但当您同时为所有的getter和setter实现时,任何可能的优势都会被否定 你需要两个循环;一个用于查找最小值和最大值,一个用于显示学生。应遵守Java命名约定,类名以大写字母开头。还有一个getGpadouble应该是setter 修复所有这些,它可能看起来像
public class App {
public static class Student {
private String firstName;
private String lastName;
private int age;
private double gpa;
public Student(String a, String b, int c, double d) {
firstName = a;
lastName = b;
age = c;
gpa = d;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fn) {
firstName = fn;
}
public String getLastName() {
return lastName;
}
public void setLastName(String ln) {
lastName = ln;
}
public int getAge() {
return age;
}
public void setAge(int x) {
age = x;
}
public double getGpa() {
return gpa;
}
public void setGpa(double g) {
gpa = g;
}
}
public static void main(String[] args) throws Exception {
Student st1 = new Student("Rebecca", "Collins", 22, 3.3);
Student st2 = new Student("Alex", "White", 19, 2.8);
Student st3 = new Student("Jordan", "Anderson", 22, 3.1);
Student[] studentArray = { st1, st2, st3 };
Student maxStudent = studentArray[0];
Student minStudent = studentArray[0];
for (int i = 1; i < studentArray.length; i++) {
if (studentArray[i].getGpa() > maxStudent.getGpa()) {
maxStudent = studentArray[i];
}
if (studentArray[i].getGpa() < minStudent.getGpa()) {
minStudent = studentArray[i];
}
}
System.out.printf("The highest GPA is: %.1f%n", maxStudent.getGpa());
System.out.printf("The lowest GPA is: %.1f%n", minStudent.getGpa());
for (Student s : studentArray) {
System.out.printf("Name: %s %s%n", s.getFirstName(), s.getLastName());
System.out.printf("Age: %d%n", s.getAge());
System.out.printf("GPA: %.1f%n", s.getGpa());
}
}
}
而且,如果您使用的是Java8+,您可以通过流式处理学生,映射到gpa值,然后收集统计数据来简化主代码。像
public static void main(String[] args) throws Exception {
Student st1 = new Student("Rebecca", "Collins", 22, 3.3);
Student st2 = new Student("Alex", "White", 19, 2.8);
Student st3 = new Student("Jordan", "Anderson", 22, 3.1);
Student[] studentArray = { st1, st2, st3 };
DoubleSummaryStatistics dss = Arrays.stream(studentArray).mapToDouble(Student::getGpa).summaryStatistics();
System.out.printf("The highest GPA is: %.1f%n", dss.getMax());
System.out.printf("The lowest GPA is: %.1f%n", dss.getMin());
Arrays.stream(studentArray).map(s -> String.format("Name: %s %s%nAge: %d%nGPA: %.1f", //
s.getFirstName(), s.getLastName(), s.getAge(), s.getGpa())).forEach(System.out::println);
}
您是否遇到编译器错误?您正在运行什么命令以及确切的错误消息是什么?您发布的代码将无法编译;大括号不匹配,Java中没有关键字var。我没有看到任何文件I/O或代码中任何地方都能产生消息到达文件结尾,而如果你在文件的末尾添加了2个缺失的文件,当它期望更多的源代码时,它不会到达文件的末端,也就是在解析的中间!嗨,欢迎来到StackOverflow!您提供的代码不从文件中读取,因此可能无法引发异常。请检查您是否提供了正确的代码,以及您的问题是否与应用程序的不同部分有关。您所说的var关键字不存在是什么意思?它是在Java10中添加的,并在Java11中扩展到lambdas。@Andreas编辑过。。。Java10之前的版本,等等。
public static void main(String[] args) throws Exception {
Student st1 = new Student("Rebecca", "Collins", 22, 3.3);
Student st2 = new Student("Alex", "White", 19, 2.8);
Student st3 = new Student("Jordan", "Anderson", 22, 3.1);
Student[] studentArray = { st1, st2, st3 };
DoubleSummaryStatistics dss = Arrays.stream(studentArray).mapToDouble(Student::getGpa).summaryStatistics();
System.out.printf("The highest GPA is: %.1f%n", dss.getMax());
System.out.printf("The lowest GPA is: %.1f%n", dss.getMin());
Arrays.stream(studentArray).map(s -> String.format("Name: %s %s%nAge: %d%nGPA: %.1f", //
s.getFirstName(), s.getLastName(), s.getAge(), s.getGpa())).forEach(System.out::println);
}