Java can';t从Arraylist中删除对象用户
有人能帮我写代码吗。我是个乞丐,这个叫java的东西让我很困惑:)我的问题是我必须删除用户,但我的输出总是找不到用户名。。。提前谢谢Java can';t从Arraylist中删除对象用户,java,Java,有人能帮我写代码吗。我是个乞丐,这个叫java的东西让我很困惑:)我的问题是我必须删除用户,但我的输出总是找不到用户名。。。提前谢谢 public void removeUser() { java.util.Scanner input = new java.util.Scanner(System.in); int checks = 1; if (checks == 1) { for (int i = 0; i < userList().size();
public void removeUser() {
java.util.Scanner input = new java.util.Scanner(System.in);
int checks = 1;
if (checks == 1) {
for (int i = 0; i < userList().size(); i++) {
System.out.println("Input user name for the account you want to be deleted");
userName = input.next();
if (userList.equals(userName)) {
userList.get(i);
userList.remove(userName);
System.out.println("You succesfully removed user acount");
System.out.println("If you want to exit press 0, if you want to continue press 1");
checks = input.nextInt();
} else {
System.out.println("User name not found");
}
}
}
if (checks == 0) {
administrator();
}
}
public void removeUser(){
java.util.Scanner输入=新的java.util.Scanner(System.in);
整数=1;
如果(检查==1){
对于(int i=0;i
为什么您认为这会起作用
if (userList.equals(userName))
?
也许试着把它去掉
boolean removed = userList.remove(userName);
if (removed) {
System.out.println("You succesfully removed user acount");
}
不需要循环
看
所以你的代码看起来像
java.util.Scanner input = new java.util.Scanner(System.in);
int checks = 1;
while (checks == 1) {
System.out.println("Input user name for the account you want to be deleted");
userName = input.next();
if (userList.remove(userName)) {
System.out.println("You succesfully removed user acount");
}
else {
System.out.println("User name not found");
}
System.out.println("If you want to exit press 0, if you want to continue press 1");
checks = input.nextInt();
}
这里您对代码所做的第一件事是将
if(checks==1)
更改为while
循环while(checks==1)
,因为if
只能执行一次
第二件事if(userList.equals(userName))
永远不会true
,因此if
子句将不会执行。您首先从列表中获取用户名,如下所示String name userList.get(i)代码>然后现在你可以检查它是否像这样相等
if(name.equals(userName)) //or userList.contains(userName){
// userList.remove(userName);
// OR
// userList.remove(i);
}
Eidt:
您可以按以下方式更改代码,也许可以为您工作
List<String> userList = new ArrayList<>();
userList.add("AA");
userList.add("BB");
userList.add("CC");
java.util.Scanner input = new java.util.Scanner(System.in);
int checks = 1;
while (checks == 1) {
for (int i = 0; i < userList.size(); i++) {
System.out.println("Input user name for the account you want to be deleted");
String userName = input.next();
if(userList.remove(userName)) {
System.out.println("You scornfully removed user account");
} else {
System.out.println("User name not found");
}
System.out.println("If you want to exit press 0, if you want to continue press 1");
checks = input.nextInt();
}
}
if (checks == 0) {
administrator();
}
List userList=new ArrayList();
用户列表。添加(“AA”);
用户列表。添加(“BB”);
userList.add(“CC”);
java.util.Scanner输入=新的java.util.Scanner(System.in);
整数=1;
while(检查==1){
对于(int i=0;i
公共静态void main(字符串[]args){
List customerNames=new ArrayList();
客户名称。添加(“约翰”);
客户名称。添加(“莉莉”);
添加(“德鲁伊”);
java.util.Scanner输入=新的java.util.Scanner(System.in);
System.out.println(“请输入1”);
if(input.nextInt()!=1){
System.out.println(“中断检查…”);
返回;
}
对于(int i=0;i
公共静态void main(字符串[]args){
List customerNames=new ArrayList();
客户名称。添加(“约翰”);
客户名称。添加(“莉莉”);
添加(“德鲁伊”);
java.util.Scanner输入=新的java.util.Scanner(System.in);
System.out.println(“请输入1:”);
if(input.nextInt()!=1){
System.out.println(“中断检查…”);
返回;
}
System.out.println(“===================开始==============”);
System.out.println(“请输入1:”);
while(input.nextInt()!=0){
System.out.println(“输入要删除的帐户的用户名……”);
System.out.println(“输入名称:”);
字符串_name=input.next();
对于(int i=0;i
我们真的需要查看userList
的代码问题可能在那里,而不是在您显示的代码中。userList
实际包含的数据类型是什么?而且,userList.equals(userName)
不太可能是真的,也许您的意思是userList.contains(userName)
public static void main(String[] args) {
List<String> customerNames = new ArrayList<String>();
customerNames.add("john");
customerNames.add("lily");
customerNames.add("druid");
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter 1 ");
if(input.nextInt() != 1){
System.out.println("break checks ... ...");
return;
}
for(int i = 0; i < customerNames.size(); i++) {
System.out.println("Input user name for the account you want to be deleted");
if (customerNames.get(i).equals(input.next())) {
customerNames.remove(i);
System.out.println("You succesfully removed user acount");
System.out.println("If you want to exit press 0 ... ...\n");
if(input.nextInt() == 0){ //break
break;
}
} else {
System.out.println("User name not found... ...\n");
}
}
}
public static void main(String[] args) {
List<String> customerNames = new ArrayList<String>();
customerNames.add("john");
customerNames.add("lily");
customerNames.add("druid");
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter 1: ");
if(input.nextInt() != 1){
System.out.println("break checks ... ...");
return;
}
System.out.println("========= start =========");
System.out.println("Please enter 1: ");
while(input.nextInt() != 0){
System.out.println("Input user name for the account you want to be deleted... ...");
System.out.println("enter name:");
String _name = input.next();
for(int i = 0; i < customerNames.size(); i++) {
if(_name.equals(customerNames.get(i))){
customerNames.remove(_name);
System.out.println("You succesfully removed user acount");
break;
}
}
System.out.println("If you want to exit press 0 ... ...\n");
System.out.println("input numeric:");
}
System.out.println("========= end =========");
//break
if(customerNames.size() == 0){return;}
for(String name : customerNames){//print names
System.out.println(name);
}
}