最后我多次收到错误消息,但我不知道为什么。您能否解释一下原因,也许可以帮助我找到解决方案?
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayListUserDefinedObjectExample { // defining class
public static void main(String[] args) {
List<User> users = new ArrayList<>(); // setting arraylist
users.add(new User("David", "Pot" , 17, "male", "Armstrong Str. 24", "Griesheim, 57401", "Hessen")); // getting info of the given student
users.add(new User("John","Lok", 20, "male", "Madison Str. 76", "Darmstadt, 19050", "Hessen")); // getting info of the given student
users.add(new User("Daniel","Wild", 15, "male", "Gartner Str. 39", "Darmstadt, 34012", "Hessen")); // getting info of the given student
users.add(new User("Martin","Mill", 19, "male", "Willow Str. 12", "Offenbach, 45012", "Hessen")); // getting info of the given student
users.add(new User("Jake","Mill", 19, "male", "Willow Str. 12", "Offenbach, 45012", "Hessen")); // getting info of the given student
System.out.println("EKS administration");
System.out.println("----------------------------------");
users.remove(3); // removing object at the given index
users.add(2,new User("Logan","Paul", 18, "male", "Maximilian Str. 90", "Offenbach, 45012", "Hessen" )); // adding student at the given index
User u = new User();
System.out.println("Enter the name of the student that you are looking for: "); // user enters the student name
Scanner scanner = new Scanner(System.in);
u.setName(scanner.nextLine()); // the machine is searching all the names for the right one
for (User user : users){
if (u.getName().equalsIgnoreCase(user.getName())){
System.out.println("The name of the student is " + user.getName() +"\n" + user.getLastname()+ " and this student is " + user.getAge() +" years old" + "."+ "This student is a " + user.getSex() + "." + "\n" +"House adress of this student:" + user.getStreet()+ "\n"+ "City and postcode:"+ user.getPlace()+ "\n"+ "State:"+ user.getState());
} // user gets all the info for the student that he searched
else {
System.out.println("----------------------------------");
System.err.println("You gave a wrong name");
}
}
}
}
答案 0 :(得分:0)
请检查下面的代码。 因此,我首先要添加的是一个空的构造函数,以便您可以创建一个没有数据的对象。 然后在扫描仪的帮助下,代码将等待您输入名字(按Enter)。对于每个将遍历您的列表并比较名称。命中时它将返回该对象。
class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class ArrayListUserDefinedObjectExample {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User("David", 31));
users.add(new User("John", 24));
users.add(new User("Daniel", 15));
User u = new User();
System.out.println("Enter your name: ");
Scanner scanner = new Scanner(System.in);
u.setName(scanner.nextLine());
for (User user : users){
if (u.getName().equalsIgnoreCase(user.getName())){
System.out.println("Your name is " + user.getName() + " and your age is " + user.getAge() +".");
}
}
}
}