首页 > 解决方案 > java - 如果我在彼此之后添加 2 或 3,arraylist.add 函数将被忽略

问题描述

所以我对一般编程相当陌生,我想制作一个简单的学生数据库系统,使用数组列表和方法,问题是我的 add_inf() 方法有问题,它忽略了我的名字.add(in.nextLine ); 并且不接受任何输入并直接进入第二个命令这里是完整的代码:

package advanced.db;

import java.util.ArrayList;
import java.util.Scanner;

public class AdvancedDb {

    static Scanner in = new Scanner(System.in);
    static ArrayList<String> st_name = new ArrayList<>();
    static ArrayList<String> age = new ArrayList<>();
    static ArrayList<String> address = new ArrayList<>();

public static void add_info(){

    System.out.print("enter Name :");
    st_name.add(in.nextLine());

    System.out.print("enter age :");
    age.add(in.nextLine());
    
    System.out.print("enter address :");
    address.add(in.nextLine());
        
    }

    public static void view() {
        for (int x = 0; x < st_name.size() && x < age.size() && x < address.size(); x++) {

            System.out.println(st_name.get(x));
            System.out.println(age.get(x));
            System.out.println(address.get(x) + "\t");

        }
        if (st_name.isEmpty() && age.isEmpty() && address.isEmpty()) {
            System.out.println("list is empty");
        }
    }

    public static void delete() {
        System.out.println("please enter the name you want to be deleted");
        String input;

        input = in.nextLine();
        for (int x = 0; x < st_name.size() && x < age.size() && x < address.size(); x++) {
            if (input.equals(st_name.get(x))) {
                System.out.println("info of the name (" + input + ")\n is deleted");
                st_name.remove(x);
                age.remove(x);
                address.remove(x);
            }
        }
    }

    public static void search() {
        System.out.println("enter the name or age you're searching for  ");
        String input;

        input = in.nextLine();
        for (int x = 0; x < st_name.size() && x < age.size() && x < address.size(); x++) {
            if (input.equals(st_name.get(x))) {
                System.out.println("INFO regarding " + input + "is found");
                System.out.println("name :" + st_name.get(x));
                System.out.println("age  :" + age.get(x));
                System.out.println("address :" + address.get(x));
            } else if (input.equals(age.get(x))) {
                System.out.println("students with the age (" + input + ")");
                System.out.println("name :" + st_name.get(x));
                System.out.println("age  :" + age.get(x));
                System.out.println("address :" + address.get(x) + "\t");
            } else {
                System.out.println("nothing is found regarding the input");
            }
        }
            if (st_name.isEmpty() && age.isEmpty() && address.isEmpty()) {
            System.out.println("list is empty");
            }

    }

    public static void main(String[] args) {

        int x = 0;
        System.out.println("welcome\n\n");
        System.out.println("what do you want to do ");
        System.out.println("1-view the lsit of students ");
        System.out.println("2-add a new student");
        System.out.println("3-delete a student ");
        System.out.println("4-search for student ");
        System.out.println("5-exit \n");
        while (x == 0) {
            System.out.print("choice :");

             switch (in.nextInt()) {
                case 1:
                    view();
                    break;
                case 2:
                    add_info();
                  
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    search();
                    break;
                case 5:
                    x = 1;
                    System.out.println("exiting now");
                    break;

                default:
                    System.out.println("incorrect input try again");
                    break;
            }
        }

    }

}

出于某种原因,当您按 2 作为您的选择时,输出是 name:age: address: 仅考虑年龄和地址 我尝试删除 name.add(in.nextLine) 并留下年龄和地址如果有人帮助我理解为什么会发生这种情况,我会非常感激

标签: java

解决方案


推荐阅读