public class Student{ private String stuld;public void setStuld(String stuld) throws Exceptionl{ ;if (stuld. length()!=4) { throws new Exception("学号必须为4位!") }else{ this. stuld = stuld; } }这段代码哪里出错
时间: 2023-05-16 18:06:24 浏览: 61
这段代码在 setStuld 方法中缺少分号,应该在 if 语句的后面加上分号。正确的代码应该是:
public class Student {
private String stuld;
public void setStuld(String stuld) throws Exception {
if (stuld.length() != 4) {
throw new Exception("学号必须为4位!");
} else {
this.stuld = stuld;
}
}
}
相关问题
public class Student { private String name; private int id; private List<Book> borrowedBooks; private int count; public Student(String name, int id) { this.name = name; this.id = id; this.borrowedBooks = new ArrayList<>(); this.count = 0; } public void borrowBook(Book book) throws Exception { if (this.count >= 10) { throw new Exception("已经借阅了最大数量的书籍"); } this.borrowedBooks.add(book); this.count++; } public void returnBook(Book book) { this.borrowedBooks.remove(book); this.count--; }}public class Book { private String title; private String author; private String isbn; public Book(String title, String author, String isbn) { this.title = title; this.author = author; this.isbn = isbn; } // 省略 getter 和 setter 方法}
以上代码定义了两个类:Student 和 Book。Student 类有一个私有的字符串类型的 name 属性,一个整型的 id 属性,一个 List<Book> 类型的 borrowedBooks 属性以及一个整型的 count 属性。该类定义了一个构造方法,用于初始化这些属性。该类还定义了 borrowBook 方法和 returnBook 方法,用于借书和还书。如果 borrowBook 方法中的 count 属性已经达到最大值 10,则会抛出一个异常。Book 类有三个私有的字符串类型的属性:title、author 和 isbn,以及一个构造方法用于初始化这些属性。此外,该类还省略了 getter 和 setter 方法。
帮我优化以下代码import java.io.*; import java.time.LocalDate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo5 { public static void main(String[] args) throws IOException { ArrayList<Student> arr = new ArrayList<>(); File f1 = new File("input.txt"); FileInputStream fin = new FileInputStream(f1); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String msg = br.readLine(); while (msg != null) { if (msg.equals("")) { msg = br.readLine(); continue; } String[] stu = msg.split(" "); String num = stu[0]; String na = stu[1]; String da = stu[2]; String gen = stu[3]; Student student = new Student(Integer.parseInt(num), na,LocalDate.parse(da), gen); arr.add(student); Collections.sort(arr,Comparator.comparing(Student::getDate)); msg = br.readLine(); } br.close(); fin.close(); FileWriter writer = new FileWriter("output.txt"); for (Student obj:arr ) { writer.write(String.valueOf(obj)); writer.write("\r\n"); } writer.close(); } static class Student { private Integer number; private String name; private LocalDate date; private String gender; public LocalDate getDate() { return date; } public Student(Integer number, String name, LocalDate date, String gender) { this.number = number; this.name = name; this.date = date; this.gender = gender; } @Override public String toString() { return number + " " + name + " " + date + " " + gender; } } }
这段代码主要有两个问题:
1. 没有使用 try-with-resources 语句处理文件流
2. 每次读取一行数据时,都需要进行字符串的操作,可能会影响性能
下面是我对代码进行的优化:
```java
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Demo5 {
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"));
FileWriter writer = new FileWriter("output.txt")) {
String msg;
while ((msg = br.readLine()) != null) {
if (msg.trim().isEmpty()) {
continue;
}
String[] stu = msg.split(" ");
int num = Integer.parseInt(stu[0]);
String na = stu[1];
LocalDate da = LocalDate.parse(stu[2]);
String gen = stu[3];
Student student = new Student(num, na, da, gen);
arr.add(student);
Collections.sort(arr, Comparator.comparing(Student::getDate));
}
for (Student obj : arr) {
writer.write(String.valueOf(obj));
writer.write(System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class Student {
private Integer number;
private String name;
private LocalDate date;
private String gender;
public LocalDate getDate() {
return date;
}
public Student(Integer number, String name, LocalDate date, String gender) {
this.number = number;
this.name = name;
this.date = date;
this.gender = gender;
}
@Override
public String toString() {
return number + " " + name + " " + date + " " + gender;
}
}
}
```
优化后的代码使用 try-with-resources 语句处理文件流,避免了手动关闭流的问题。同时,将读取文件和写入文件的逻辑放在了同一个 try-with-resources 语句块中,避免了代码中的重复。读取文件时,使用了 `trim()` 方法去掉字符串两端的空格,并且使用 `isEmpty()` 方法判断是否为空,避免了每次读取数据时进行字符串判断的问题。写入文件时,使用了 `System.lineSeparator()` 方法代替了 `"\r\n"`,这样可以在不同的操作系统上保持一致的换行符。