简体   繁体   中英

Here is my final code. I am getting in the end the error message multiple times and I don´t know why

I am getting in the end the error message multiple times and I don´t know why. Can you please explain me why and maybe help me find the solution to fix it?

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

Please check the code below. So first thing I would add is an empty constructor, so you can create an object without data. Then by the help of the scanner the code will wait for you to enter first name (press enter). For each will iterate over your list and compare the names. When there is a hit it will return that object.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM