gpt4 book ai didi

java - 对类的数组进行冒泡排序

转载 作者:行者123 更新时间:2023-12-01 14:03:46 25 4
gpt4 key购买 nike

我编写了一个程序,该程序应该从文件中读取记录并将它们输入到 Student 类的数组中。然后我需要按名称对它们进行排序。

import java.io.*;
import java.util.Scanner;

public class StudentTest
{

public static void main(String[] args)
{
String name;
String address;
String major;
double gpa;
int classLevel;
int college;
String blank;
String idNumber;

Scanner fileIn = null;
try
{
fileIn = new Scanner (new FileInputStream("student.dat"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
System.exit(0);
}

Student[] aStudent = new Student[15];
int index = 0;

for (index=0; index <= 15; index++)
{
while (fileIn.hasNext())
{
name = fileIn.nextLine();
address = fileIn.nextLine();
major = fileIn.nextLine();
gpa = fileIn.nextDouble();
classLevel = fileIn.nextInt();
college = fileIn.nextInt();
fileIn.nextLine();
idNumber = fileIn.nextLine();
aStudent[index] = new Student(name, address, major, gpa, classLevel, college, idNumber);
aStudent[index].Display();
}
}
Student temp = null;
for (int pass = 0; pass < (index-1); pass++)
{
for (int c = 0; c < (index - 1); c++)
{
if (aStudent[].getName() > aStudent[c+1].getName())
{
temp = aStudent[];
aStudent[]=aStudent[+1];
aStudent[+1]=temp;
}
}
}
}
}

import java.util.Scanner;
public class Student
{
private String name;
private String address;
private String major;
private double gpa;
private int classLevel;
private int college;
private String idNumber;
Scanner keyboard = new Scanner(System.in);

public Student(String name, String address, String major, double gpa, int classLevel, int coll, String idNum)
{
this.name = name;
this.address = address;
this.gpa = gpa;
this.major = major;
this.classLevel = classLevel;
this.college = coll;
this.idNumber = idNum;

}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getMajor()
{
return major;
}
public double getGPA()
{
return gpa;
}
public int getClassLevel()
{
return classLevel;
}
public int getCollege()
{
return college;
}
public String getID()
{
return idNumber;
}
public void setAddress(String address)
{
}
public void setMajor(String maj)
{
}
public void setCollege(int coll)
{
}
public void Display()
{
System.out.println("Name: "+getName());
System.out.println("Address: "+getAddress());
System.out.println("Major: " + getMajor());
System.out.println("GPA: "+getGPA()+" Class Level: "+getClassLevel()+" College: "+getCollege());
System.out.println("ID: "+getID());
System.out.println("===============================");
}
}

我按照教授在类里面描述的方式编写了排序,但我仍然收到错误“> 运算符对于参数类型 java.laungage.String 未定义”

任何帮助将不胜感激。

谢谢

编辑:我采用了 Ashan 的建议,现在看起来像这样。

    for (int pass = 0; pass < (index-1); pass++)
{
for (int c = 0; c < (index - 1); c++)
{
if (aStudent[c].getName().compareTo(aStudent[c+1].getName()) > 0)
{
temp = aStudent[c];
aStudent[c]=aStudent[+1];
aStudent[+1]=temp;

这清除了该错误。但是,我现在收到 NullPointerException。

最佳答案

您不能使用 < 、 > 等运算符来比较字符串。为了比较字符串,String 类中提供了一个名为 compareTo 的方法。此方法按字典顺序比较两个字符串。

compareTo返回

  • 如果两个字符串按字典顺序相等,则为 0
  • 如果调用字符串按字典顺序小于输入字符串,则为-1
  • 如果调用字符串按字典顺序大于输入的 Stirng,则为 1

您可以替换以下条件

    if  (aStudent[].getName() > aStudent[c+1].getName())

使用compareTo方法:

    if  (aStudent[].getName().compareTo(aStudent[c+1].getName()) > 0)

关于java - 对类的数组进行冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com