Java数组对象操作求助:Student类实现及数组元素匹配查询逻辑困惑
Hey there! Your Student class is already looking great—you’ve nailed the constructors, getters/setters, and toString method perfectly. Let’s fill in the missing logic to handle those two query tasks you need:
Task 1: Find Students by Input Address
First, we’ll prompt the user to enter an address, then loop through the Student array to check which students have a matching address. Remember: for String comparisons in Java, always use the equals() method (not ==) because == checks reference equality, while equals() verifies actual string content.
Task 2: Find Students with Gmail Addresses
Next, we’ll loop through the array again and use the contains() method on the email string to check if it includes "gmail.com".
Updated Full Code
Here’s your code with the missing logic added and explained:
import java.util.*; public class Student{ private String name; private int age; private String address; private String email; public Student(){} public Student(String name, int age, String address, String email){ this.name = name; this.age = age; this.address = address; this.email = email; } public void setName(String newName){ name = newName; } public void setAge(int newAge){ age = newAge; } public void setAddress(String newAddress){ address = newAddress; } public void setEmail(String newEmail){ email = newEmail; } public String getName(){ return name; } public int getAge(){ return age; } public String getAddress(){ return address; } public String getEmail(){ return email; } public String toString() { return "Name: " + name + ", Age: " + age + ", Address: " + address + ", Email: " + email; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); Student[] students = new Student[5]; // Collect 5 students' info for(int i = 0; i < 5; i++){ System.out.println("\nEnter details for Student " + (i+1) + ":"); System.out.print("Name: "); String name = sc.nextLine(); System.out.print("Age: "); int age = sc.nextInt(); sc.nextLine(); // Consume the leftover newline character System.out.print("Address: "); String address = sc.nextLine(); System.out.print("Email: "); String email = sc.nextLine(); students[i] = new Student(name, age, address, email); } // Task 1: Find students matching the input address System.out.println("\n--- Students Matching Input Address ---"); System.out.print("Enter an address to search: "); String searchAddress = sc.nextLine(); boolean foundAddressMatch = false; // Use enhanced for loop for cleaner iteration for(Student student : students){ if(student.getAddress().equals(searchAddress)){ System.out.println(student); foundAddressMatch = true; } } // Handle case where no matches exist if(!foundAddressMatch){ System.out.println("No students found living at this address."); } // Task 2: Find students with gmail.com emails System.out.println("\n--- Students with Gmail Addresses ---"); boolean foundGmail = false; for(Student student : students){ if(student.getEmail().contains("gmail.com")){ System.out.println(student); foundGmail = true; } } // Handle case where no gmail users exist if(!foundGmail){ System.out.println("No students with gmail.com email addresses found."); } sc.close(); } }
Key Explanations:
- Enhanced For Loop: Used
for(Student student : students)to iterate through the array more cleanly than a traditional index-based loop. - String Equality Check:
equals()ensures we’re comparing the actual text of addresses, not just memory references. - Email Substring Check:
contains("gmail.com")detects the substring anywhere in the email (works for formats likeuser@gmail.comoruser+tag@gmail.com). - Edge Case Handling: Added checks to print a clear message if no matches are found, so the user doesn’t get a blank output.
内容的提问来源于stack exchange,提问作者flopprfr

