【问题标题】:How to fix "reached end of file while parsing"如何修复“解析时到达文件末尾”
【发布时间】:2019-12-31 02:57:39
【问题描述】:

我正在编写一个程序来查找并显示具有最高 GPA 的学生,以及具有 4 个属性(名字、姓氏、年龄、GPA)的班级中 GPA 最低的学生。

我的代码输出结果为“构建成功”,但解析错误仍然出现,并且没有正确的输出信息。

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;

     }

 } 

我将不胜感激任何解决错误的见解以及我可以做些什么来使此代码正常工作的解决方案。

【问题讨论】:

  • 您是否遇到编译器错误?您正在运行什么命令,确切的错误消息是什么?
  • 您发布的代码无法编译;大括号不匹配,Java 中没有关键字var。我没有看到任何文件 I/O 或代码中可能产生“解析时到达文件末尾”消息的任何位置
  • 如果您在文件末尾添加 2 个缺少的 },则当它期待更多源代码时,即它仍在中间时,它不会到达文件末尾解析!!!
  • 您好,欢迎来到 StackOverflow!您提供的代码不会从文件中读取,因此(可能)无法抛出异常。请检查您提供的代码是否正确,并且您的问题不在于应用程序的其他部分。

标签: java arrays loops class


【解决方案1】:

您发布的代码存在许多(可能的)问题;您似乎正在使用内部 student 类;您的大括号不匹配并且您的缩进似乎不一致,并且 var 关键字在 Java 10 之前的版本中不存在(我们不知道您安装的 JDK 或配置的项目编译器级别)。您的 student(s) 不应有单独的 minmax 字段。

对于包含所有public 字段的类的优点存在一些争论;但是,当您还为所有这些实现 getter 和 setter 时,任何可能的优势都会被否定。

你需要两个循环;一种是查找minmax,一种是显示student(s)。应遵守 Java 命名约定(类名以大写字母开头)。你有一个getGpa(double) 应该是一个二传手。

修复所有这些,它可能看起来像

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());
        }
    }
}

我跑的;生产

The highest GPA is: 3.3
The lowest GPA is: 2.8
Name: Rebecca Collins
Age: 22
GPA: 3.3
Name: Alex White
Age: 19
GPA: 2.8
Name: Jordan Anderson
Age: 22
GPA: 3.1

并且,如果您使用的是 Java 8+,您可以使用 DoubleSummaryStatistics 简化 main 代码,通过流式传输 Student(s),映射到 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);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多