使用java创建更好的文本的基础知识



有些版本的java编译器尚未实现Vector类的sort((方法。如果您没有使用JDK1.8或更高版本,请使用Collections类对Vector数据进行排序。低于的样品使用

有些版本的java编译器还没有实现Vector类的sort((方法。如果您没有使用JDK1.8或更高版本,请使用Collections类对Vector数据进行排序。低于的样品使用

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
try {
BufferedReader br = new BufferedReader(new FileReader("c://student.txt"));
char g;
int yl, I;
String ln, fn, id, cors, con;
Student v[] = new Student[4];
Scanner sc = new Scanner(System.in);
boolean en = true;
boolean ent = true;
for (i = 0; i < 4; i++) {
id = br.readLine();
ln = br.readLine();
fn = br.readLine();
g = br.readLine().charAt(0);
cors = br.readLine();
yl = Integer.parseInt(br.readLine());
v[i] = new Student(ln, fn, id, cors, g, yl);
}
while (en == true) {
System.out.println("--------------------------------------");
System.out.println("--------------------------------------");
System.out.println("1. Name");
System.out.println("2. Course then Name");
System.out.println("3. Year Level then Name");
System.out.println("4. Course then Year Level and the Name");
System.out.println("5. Exit");
System.out.println("--------------------------------------");
System.out.println("--------------------------------------");
System.out.println("Choose Menu: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
Arrays.sort(v);
display_array(v);
break;
case 2:
Arrays.sort(v);
display_array(v);
break;
case 3:
Arrays.sort(v);
display_array(v);
break;
case 4:
Arrays.sort(v);
display_array(v);
break;
case 5:
en = false;
System.out.println("nn nTHANK YOU FOR USING THE PROGRAM!!");
break;
}
if (en != false) {
System.out.println("Press [Enter key] to continue");
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e) {
System.err.println("File not found");
} catch (IOException e) {
System.err.println("Unable to read the file.");
}
}
public static void display_array(Student arr_v[]) throws IOException {
System.out.println("--------------------------------------");
for (int i = 0; i < arr_v.length; i++) {
arr_v[i].display();
}
System.out.println("--------------------------------------");
}
}
public class Student implements Comparable {
private String lastname, firstname, studentid, course;
private char gender;
private int yearlevel;
public Student(String ln, String fn, String id, String cors, char g, int yl) {
lastname = ln;
firstname = fn;
studentid = id;
course = cors;
gender = g;
yearlevel = yl;
}
public int compareTo(Object anotherObject) {
Student anotherStudent = (Student) anotherObject;
int compareResult =
this.course.compareTo(anotherStudent.lastname);
if (compare)
return 0;
}
public void display() {
System.out.printf("ID: %-8s  Name: %-20s  Sex: %c  Course: %-8s  Year: %dn", studentid, (lastname + ", " + firstname), gender, course, yearlevel);
}
public void setGender(char gender) {
this.gender = gender;
}
public char getGender() {
return gender;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return lastname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return firstname;
}
public void setStudentId(String studentid) {
this.studentid = studentid;
}
public String getStudentId() {
return studentid;
}
public void setCourse(String course) {
this.course = course;
}
public String getCourse() {
return course;
}
public void setYearLevel(int yearlevel) {
this.yearlevel = yearlevel;
}
public int getYearLevel() {
return yearlevel;
}
}

所以你的代码中实际上有两个错误:

  1. 从测试中读取
id = br.readLine();
ln = br.readLine();
fn = br.readLine();
g = br.readLine().charAt(0);
cors = br.readLine();
yl = Integer.parseInt(br.readLine());
  1. Comparable

1.当您读取line时,它会读取整行,甚至是spaces,所以当您写入br.readLine()时,它将一次性读取20192215 Ang Bryan m BSCS 4整行
而你所做的是,你称之为6 times,而里面没有足够的lines
你可以做的是。当你遇到space时,在array中读取一行和split,然后用index得到20192215 Ang Bryan m BSCS 4。下面是你应该做的。

int yl;
String ln, fn, id, cors, con,g;
for (int i = 0; i < 4; i++) {
String line = br.readLine();
String studentInfo[] = line.split(" ");
id = studentInfo[0];
ln = studentInfo[1];
fn = studentInfo[2];
g  = studentInfo[3];
cors = studentInfo[4];
yl = Integer.parseInt(studentInfo[5]);
v[i] = new Student(ln, fn, id, cors, g, yl);
}

这里CCD_ 12将该字符串拆分为CCD_

  1. 当您使用Comparable时,这意味着您希望您的object只在one way中排序,CCD_17可以是nameyear或其他任何类型
    但只能通过一种方式nameyear或其他方式。它不会改变自己的行为
    然而,如果您使用Comparator,它允许您以多种方式对对象进行排序。它克服了CCD_ 23的问题


这就是使用Comparator的方法

class sortName implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String fname1 = o1.getFirstname();
String fname2 = o2.getFirstname();
return fname1.compareTo(fname2);
}
}

根据所选选项,将您的选项设置为要排序的位置。选项1:

class sortName implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String fname1 = o1.getFirstname();
String fname2 = o2.getFirstname();
return fname1.compareTo(fname2);
}
}

选项2:

class sortCourse_name implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String firstCourse = o1.getCourse();
String secondCourse = o2.getCourse();
String fname1 = o1.getFirstname();
String sname2 = o2.getFirstname();
if(firstCourse.compareTo(secondCourse) == 0) {
return fname1.compareTo(sname2);
}else {
return firstCourse.compareTo(secondCourse);
}
}
}

选项3:

class sort_Year_Name implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int firstYear = o1.getYearLevel();
int secondYear = o2.getYearLevel();
String fname = o1.getFirstname();
String sname = o2.getFirstname();
if(firstYear - secondYear == 0) {
return fname.compareTo(sname);
}else {
return firstYear - secondYear;
}
}
}

选项4:

class sort_Course_Year_Name implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String fcourse = o1.getCourse();
String scourse = o2.getCourse();

int firstYear = o1.getYearLevel();
int secondYear = o2.getYearLevel();

String fname = o1.getFirstname();
String sname = o2.getFirstname();

if(fcourse.compareTo(scourse) == 0 ) {
if(firstYear - secondYear == 0) {
return fname.compareTo(sname);
}else {
return firstYear-secondYear;
}
}else {
return fcourse.compareTo(scourse);
}

}
}

class Student的变化

class Student {
private String lastname, firstname, studentid, course, gender;
private int yearlevel;
public Student(String ln, String fn, String id, String cors, String g, int yl) {
lastname = ln;
firstname = fn;
studentid = id;
course = cors;
gender = g;
yearlevel = yl;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return gender;
}
}

完整代码:

class Main {
public static void main(String[] args) throws IOException {
try {
BufferedReader br = new BufferedReader(new FileReader("c://student.txt"));
String g; // changed
int yl;
String ln, fn, id, cors, con;
Student v[] = new Student[4];
Scanner sc = new Scanner(System.in);
boolean en = true;
boolean ent = true;
// changed
for (int i = 0; i < 4; i++) {
String line = br.readLine();
String studentInfo[] = line.split(" ");
id = studentInfo[0];
ln = studentInfo[1];
fn = studentInfo[2];
g  = studentInfo[3];
cors = studentInfo[4];
yl = Integer.parseInt(studentInfo[5]);
v[i] = new Student(ln, fn, id, cors, g, yl);
}
while (en == true) {
System.out.println("--------------------------------------");
System.out.println("--------------------------------------");
System.out.println("1. Name");
System.out.println("2. Course then Name");
System.out.println("3. Year Level then Name");
System.out.println("4. Course then Year Level and the Name");
System.out.println("5. Exit");
System.out.println("--------------------------------------");
System.out.println("--------------------------------------");
System.out.println("Choose Menu: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
// changed
Arrays.sort(v,new sortName());
display_array(v);
break;
case 2:
// changed
Arrays.sort(v, new sortCourse_name());
display_array(v);
break;
case 3:
// changed
Arrays.sort(v, new sort_Year_Name());
display_array(v);
break;
case 4:
// changed
Arrays.sort(v,new sort_Course_Year_Name());
display_array(v);
break;
case 5:
en = false;
System.out.println("nn nTHANK YOU FOR USING THE PROGRAM!!");
break;
}
if (en != false) {
System.out.println("Press [Enter key] to continue");
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e) {
System.err.println("File not found");
} catch (IOException e) {
System.err.println("Unable to read the file.");
}
}
public static void display_array(Student arr_v[]) throws IOException {
System.out.println("--------------------------------------");
for (int i = 0; i < arr_v.length; i++) {
arr_v[i].display();
}
System.out.println("--------------------------------------");
}
}
class Student {
private String lastname, firstname, studentid, course, gender;
private int yearlevel;
//changed
public Student(String ln, String fn, String id, String cors, String g, int yl) {
lastname = ln;
firstname = fn;
studentid = id;
course = cors;
gender = g;
yearlevel = yl;
}
public void display() {
System.out.printf("ID: %-8s  Name: %-20s  Sex: %s  Course: %-8s  Year: %dn", studentid, (lastname + ", " + firstname), gender, course, yearlevel);
}
// changed
public void setGender(String gender) {
this.gender = gender;
}
// changed
public String getGender() {
return gender;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return lastname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return firstname;
}
public void setStudentId(String studentid) {
this.studentid = studentid;
}
public String getStudentId() {
return studentid;
}
public void setCourse(String course) {
this.course = course;
}
public String getCourse() {
return course;
}
public void setYearLevel(int yearlevel) {
this.yearlevel = yearlevel;
}
public int getYearLevel() {
return yearlevel;
}
}
// added    
class sortName implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String fname1 = o1.getFirstname();
String fname2 = o2.getFirstname();
return fname1.compareTo(fname2);
}
}
//added
class sortCourse_name implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String firstCourse = o1.getCourse();
String secondCourse = o2.getCourse();
String fname1 = o1.getFirstname();
String sname2 = o2.getFirstname();
if(firstCourse.compareTo(secondCourse) == 0) {
return fname1.compareTo(sname2);
}else {
return firstCourse.compareTo(secondCourse);
}
}
}
//added
class sort_Year_Name implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int firstYear = o1.getYearLevel();
int secondYear = o2.getYearLevel();
String fname = o1.getFirstname();
String sname = o2.getFirstname();
if(firstYear - secondYear == 0) {
return fname.compareTo(sname);
}else {
return firstYear - secondYear;
}
}
}
//added
class sort_Course_Year_Name implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
String fcourse = o1.getCourse();
String scourse = o2.getCourse();

int firstYear = o1.getYearLevel();
int secondYear = o2.getYearLevel();

String fname = o1.getFirstname();
String sname = o2.getFirstname();

if(fcourse.compareTo(scourse) == 0 ) {
if(firstYear - secondYear == 0) {
return fname.compareTo(sname);
}else {
return firstYear-secondYear;
}
}else {
return fcourse.compareTo(scourse);
}

}
}

最新更新