Вот мой окончательный код.Я получаю сообщение об ошибке несколько раз, и я не знаю, почему - PullRequest
0 голосов
/ 17 июня 2019

Я получаю сообщение об ошибке несколько раз, и я не знаю почему. Можете ли вы объяснить мне, почему и, возможно, помочь мне найти решение, чтобы исправить это?

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

1 Ответ

0 голосов
/ 17 июня 2019

Пожалуйста, проверьте код ниже. Итак, первое, что я хотел бы добавить, это пустой конструктор, чтобы вы могли создать объект без данных. Затем с помощью сканера код будет ждать вас, чтобы ввести имя (нажмите 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() +".");
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...