字符串比较不显示所有匹配结果



我正在编写一段代码,该代码应该将类数组(users)的元素移动到新数组(ordered),如果用户的"ordered"参数设置为y。代码将适用于少数用户,但对于大多数用户,它只是跳过他们。为什么会这样?我比较字符串是错的,还是有什么我没有意识到的?

System.out.println("List of ordered phones:");
for(int i = 0; i < users.length; i++) {
if(users[i].ordered.equals("Y")) {
ordered[i] = users[i];
System.out.println(ordered[i].name);
}
}

这是正在讨论的循环。除了字符串比较之外,它没有太多的内容,但即使这样也非常直接。

下面是整个程序:

class User{
String name;
String phoneNum;
String current;
String type;
String requested;
String location;
String ordered;
User(String name, String phoneNum, String current, String type, String requested, String location, String ordered){
this.name = name;
this.phoneNum = phoneNum;
this.current = current;
this.type = type;
this.requested = requested;
this.location = location;
this.ordered = ordered;
}
public User() {

}
}
public class readWrite {
@SuppressWarnings("unlikely-arg-type")
public static void main(String[] args) throws FileNotFoundException {

User[] users = new User[149];
User[] ordered = new User[149];
User[] received = new User[149];
User[] trade = new User[149];
int count = 0;

File file = new File("I don't like showing my file path but it's correct");
Scanner scan = new Scanner(file);

while(scan.hasNextLine()) {
User user = new User(scan.nextLine(), scan.nextLine(), scan.nextLine(), scan.nextLine(), scan.nextLine(), scan.nextLine(), scan.nextLine());
users[count] = user;
count++;
}

User temp;
for(int i = 0; i < users.length; i++) {
for(int j = 0; j < users.length; j++) {
if(users[i].name.charAt(0) < users[j].name.charAt(0)) {
temp = users[i];
users[i] = users[j];
users[j] = temp;
}
}
}

for(int i = 0; i < users.length; i++) {
System.out.println(i + "." + " Name: " + users[i].name + "n" + "Phone Number: " + users[i].phoneNum + "n" + "Current Phone: " + users[i].current + "n" + "Phone Type: " + users[i].type + "n" + "Requested Phone: " + users[i].requested + "n" + "Location: " + users[i].location + "n" + "Ordered?: " + users[i].ordered + "nn");
}

System.out.println("List of ordered phones:");
for(int i = 0; i < users.length; i++) {
if(users[i].ordered.equals("Y")) {
ordered[i] = users[i];
System.out.println(ordered[i].name);
}
}

它们是否可能因为不满足以下条件而被跳过?

if(users[i].ordered.equals("Y"))

我明白了。在3个for循环中,我需要使用" order ","rec"one_answers"traded"这样过滤后的数组中就没有空项了。当我使用I作为索引时,它插入空值,因为即使不满足条件,I也总是增长。

最新更新