首页 > 解决方案 > 这是我的最终代码。最后我多次收到错误消息,我不知道为什么

问题描述

最后我多次收到错误消息,我不知道为什么。你能解释一下为什么,也许能帮我找到解决办法吗?

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

标签: java

解决方案


请检查下面的代码。所以我要添加的第一件事是一个空的构造函数,所以你可以创建一个没有数据的对象。然后在扫描仪的帮助下,代码将等待您输入名字(按 Enter)。For each 将遍历您的列表并比较名称。当命中时,它将返回该对象。

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

推荐阅读