Java阵列列表验证

Java阵列列表验证,java,Java,我需要在用户向ArrayList添加人员时添加验证。输入姓名、地址、法定国家缩写。等等,我该怎么做 我想象一个IF语句,但我不知道如何将它合并到newFriend.setAddress(sc.nextLine())中行的类型 import java.util.*; import java.io.*; import javax.swing.*; public class testAddressBook { //Personal Friend Array public stat

我需要在用户向
ArrayList
添加人员时添加验证。输入姓名、地址、法定国家缩写。等等,我该怎么做

我想象一个
IF语句
,但我不知道如何将它合并到
newFriend.setAddress(sc.nextLine())中行的类型

import java.util.*;
import java.io.*;
import javax.swing.*;


public class testAddressBook {

    //Personal Friend Array
    public static PersonalFriend newFriend = new PersonalFriend(null, null, null, null, null, null, null, null, null, null,0,null);
    public static ArrayList<PersonalFriend> FriendList = new ArrayList<PersonalFriend>();

    //Business Associate Array
    public static BusinessAssociate newBusiness = new BusinessAssociate(null, null, null, null,null, null, null, null,null, null, null, null);
    public static ArrayList<BusinessAssociate> salaryList = new ArrayList<BusinessAssociate>();


    public static ArrayList<Person> addressBook = new ArrayList<Person>();
    private static Scanner sc = new Scanner(System.in);
    private static char choice;
    private static char type;


    public static void main(String[] args) throws IOException {

        boolean switcher = true;
        do {
            System.out.println("\n\tAddress Book Menu");
            System.out.println("\n\t\tEnter A to (A)dd Person ");
            System.out.println("\t\tEnter R to (R)emove Person");
            System.out.println("\t\tEnter M to (M)odify Entry");
            System.out.println("\t\tEnter S to (S)earch Address Book ");
            System.out.println("\t\tEnter L to (L)ist ALL (sorted) ");
            System.out.println("\t\tEnter D to (D)ownload to Disk ");
            System.out.println("\t\tEnter U to (U)pload from Disk ");
            System.out.println("\t\tEnter P to (P)rint Address Book ");
            System.out.println("\t\tEnter Q to Quit");
            System.out.print("\n\tPlease enter your choice: ");
            choice = sc.nextLine().toUpperCase().charAt(0);


            while ((choice != 'A') && (choice != 'R') && (choice != 'M')  && (choice != 'S') && (choice != 'L')&& (choice != 'D')&& (choice != 'U')&& (choice != 'P')&&(choice != 'Q')) {
                System.out.println("Invalid choice!  Please select (A)dd, (D)elete, (M)odify, (S)earch, (L)ist or (Q)uit: ");
                choice = sc.nextLine().toUpperCase().charAt(0);
            }


            switch (choice) {
            case 'A' :      
                if (choice == 'A') {
                    System.out.print("\nAdd a (F)riend or (B)usiness Associate? ");
                    type = sc.nextLine().toUpperCase().charAt(0);

                    while ((type != 'F') && (type != 'B')) {
                        System.out.print("Invalid choice!  Please select (F)riend or (B)usiness Associate: ");
                        type = sc.nextLine().toUpperCase().charAt(0);
                    }


                    if (type == 'F') {

                        addPersonFriend();

                    }

                    else if (type == 'B') {
                        addBusinessPerson();
                    }
                }


                break;

            case 'R' :
                deletePerson();
                break;

            case 'M' :
                modifyPerson();
                break;

            case 'S' :

                searchPerson();

                break;

            case 'L' :
                listPerson();
                break;

            case 'D' :
                downloadDisk();
                break;

            case 'U' :
                uploadDisk();
                break;

            case 'P' :
                printAddressBook();
                break;

            case 'Q' :
                switcher = false;

                break;


            }


        }
        while (switcher != false);
        System.out.println("Session Over");

    }


    private static void addPersonFriend() {
        System.out.println("\nFollow the prompts.");
        newFriend = new PersonalFriend(null, null, null, null, null, null, null, null, null, null,0,null);
        System.out.print("\nEnter First Name: ");
        newFriend.setfName(sc.nextLine());
        System.out.print("Enter Last Name: ");
        newFriend.setlName(sc.nextLine());
        System.out.print("Enter Address: ");
        newFriend.setAddress(sc.nextLine());
        System.out.print("Enter City: ");
        newFriend.setCity(sc.nextLine());
        System.out.print("Enter State: ");
        newFriend.setState(sc.nextLine());
        System.out.print("Enter Zip: ");
        newFriend.setZip(sc.nextLine());
        System.out.print("Enter Phone Number: ");
        newFriend.setPhone(sc.nextLine());
        System.out.print("Enter Cell Phone Number: ");
        newFriend.setCellphone(sc.nextLine());
        System.out.print("Enter Email Number: ");
        newFriend.setEmail(sc.nextLine());
        System.out.print("Enter Birthday: ");
        newFriend.setBirthday(sc.nextLine());
        System.out.print("Enter Age: ");
        newFriend.setAge(sc.nextInt());
        System.out.print("Enter Zodiac: ");
        newFriend.setZodiac(sc.nextLine());
        System.out.print(" ");
        newFriend.setZodiac(sc.nextLine());
        addressBook.add(newFriend);

        System.out.println("\nYou have successfully added a new person!");
    }

    private static void addBusinessPerson() {
        System.out.println("\nFollow the prompts.");
        newBusiness = new BusinessAssociate(null, null, null, null, null, null, null, null, null, null, null, null);
        System.out.print("\nEnter First Name: ");
        newBusiness.setfName(sc.nextLine());
        System.out.print("Enter Last Name: ");
        newBusiness.setlName(sc.nextLine());
        System.out.print("Enter Address: ");
        newBusiness.setAddress(sc.nextLine());
        System.out.print("Enter City: ");
        newBusiness.setCity(sc.nextLine());
        System.out.print("Enter State: ");
        newBusiness.setState(sc.nextLine());
        System.out.print("Enter Zip: ");
        newBusiness.setZip(sc.nextLine());
        System.out.print("Enter Phone Number: ");
        newBusiness.setPhone(sc.nextLine());
        System.out.print("Enter Cell Phone Number: ");
        newBusiness.setCellphone(sc.nextLine());
        System.out.print("Enter Email Number: ");
        newBusiness.setEmail(sc.nextLine());
        System.out.print("Enter Job Title: ");
        newBusiness.setJobtitle(sc.nextLine());
        System.out.print("Enter Fax Number: ");
        newBusiness.setFax(sc.nextLine());
        System.out.print("Enter Company Name: ");
        newBusiness.setCompany(sc.nextLine());      
        addressBook.add(newBusiness);

        System.out.println("\nYou have successfully added a new person!");
    }

    private static void deletePerson() {
        System.out.println("Enter first name of person: ");
        String deleteName = sc.nextLine();

        boolean foundMod = false;
        int index2 = 0;

        while (index2 < addressBook.size() && !foundMod) {
            if ( ((Person)addressBook.get(index2)).getfName().equalsIgnoreCase(deleteName)) {
                foundMod = true;
                addressBook.remove(index2);

                System.out.println("\nYou have successfully deleted this person.");
            }

            else {
                index2++;
            }

            if (foundMod)
            {}
            else
                System.out.println("Unknown");
        }

    }

    private static void modifyPerson() {

        newBusiness = new BusinessAssociate(null, null, null, null, null, null, null, null, null, null, null, null);
        newFriend = new PersonalFriend(null, null, null, null, null, null, null, null, null, null,0,null);


        System.out.print("Enter first name of person: ");
        sc.nextLine();



        if (newBusiness instanceof BusinessAssociate) {
            System.out.println("Change this business listing:  ");
            addBusinessPerson();
        }

        if (newFriend instanceof PersonalFriend) {
            System.out.println("This is a Personal Friend:  ");
            addPersonFriend();
        }


    }



    private static void searchPerson() {

        System.out.println("Enter first name of person: ");
        String findName = sc.nextLine();

        boolean found = false;
        int index = 0;

        while (index < addressBook.size() && !found) {
            if ( ((Person)addressBook.get(index)).getfName().equalsIgnoreCase(findName)) {
                found = true;
            }

            else {
                index++;
            }

            if (found)
                System.out.println("Found: " + addressBook.get(index));
            else{}

        }


    }

    private static void listPerson() {
        System.out.println("\nThere are " + addressBook.size() + " people in this address book.\n");
        Collections.sort(addressBook);


        for (int i = 0; i < addressBook.size(); i++) {
            System.out.println(addressBook.get(i));


        }
        System.out.println();



    }


    public static void downloadDisk() { 
        File file = new File("addressBookContents.dtd");


        // Create a file
        PrintWriter output = null;
        try {
            output = new PrintWriter(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Write formatted output to the file
        output.println(addressBook);
        System.out.println("\nSaved to disk in the default folder for this program. \n");

        // Close the file
        output.close();

    }

    private static void uploadDisk() throws IOException {

        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showOpenDialog(null)
                == JFileChooser.APPROVE_OPTION) {
            // Get the selected file
            java.io.File file = fileChooser.getSelectedFile();

            // Create a Scanner for the file
            Scanner input = new Scanner(file);

            // Read text from the file
            while (input.hasNext()) {
                System.out.println(input.nextLine());
            }

            // Close the file
            input.close();
        }
        else {
            System.out.println("No file selected");
        }


    }


    @SuppressWarnings("unchecked")
    private static void printAddressBook() {

        File file = new File("addressBookContents.txt");


        // Create a file
        PrintWriter output = null;
        try {
            output = new PrintWriter(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Write formatted output to the file
        output.println(addressBook);
        System.out.println("\nPrinting Successful! \n");

        // Close the file
        output.close();

    }


}//ends class
import java.util.*;
导入java.io.*;
导入javax.swing.*;
公共类testAddressBook{
//个人好友数组
public static PersonalFriend newFriend=新PersonalFriend(null,null,null,null,null,null,null,0,null);
public static ArrayList FriendList=new ArrayList();
//业务伙伴阵列
public static BusinessAssociate newBusiness=newbusinessassociate(null,null,null,null,null,null,null,null,null);
public static ArrayList salaryList=new ArrayList();
public static ArrayList addressBook=new ArrayList();
专用静态扫描仪sc=新扫描仪(System.in);
私有静态字符选择;
私有静态字符类型;
公共静态void main(字符串[]args)引发IOException{
布尔切换器=真;
做{
System.out.println(“\n\t服装手册菜单”);
System.out.println(“\n\t\tEnter A to(A)dd Person”);
System.out.println(“\t\tEnter R to(R)emove Person”);
System.out.println(“\t\t输入M到(M)修改条目”);
System.out.println(“\t\t输入到搜索地址簿”);
System.out.println(“\t\tEnter L to(L)ist ALL(sorted)”;
System.out.println(“\t\t从D到(D)从磁盘加载”);
System.out.println(“\t\tEnter U to(U)pload from Disk”);
System.out.println(“\t\tEnter P to(P)rint地址簿”);
System.out.println(“\t\tEnter Q to Quit”);
System.out.print(“\n\t请输入您的选择:”);
choice=sc.nextLine().toUpperCase().charAt(0);
而((选择!='A')&&(选择!='R')&&(选择!='M')&(选择!='S')&&(选择!='L')&&(选择!='D')&(选择!='U')&(选择!='P')&(选择!='Q')){
System.out.println(“无效选择!请选择(A)dd、(D)elete、(M)odify、(S)earch、(L)ist或(Q)uit:”);
choice=sc.nextLine().toUpperCase().charAt(0);
}
开关(选择){
案例“A”:
如果(选项=='A'){
系统输出打印(“\n添加a(F)朋友或(B)业务助理?”);
type=sc.nextLine().toUpperCase().charAt(0);
而((类型!='F')&&(类型!='B')){
系统输出打印(“无效选择!请选择(F)朋友或(B)业务伙伴:”;
type=sc.nextLine().toUpperCase().charAt(0);
}
如果(类型='F'){
addPersonFriend();
}
else if(类型='B'){
addBusinessPerson();
}
}
打破
案例“R”:
deletePerson();
打破
案例“M”:
modifyPerson();
打破
案例S:
searchPerson();
打破
案例“L”:
listPerson();
打破
案例“D”:
下载磁盘();
打破
案例“U”:
上传磁盘();
打破
案例“P”:
printAddressBook();
打破
案例‘Q’:
切换器=假;
打破
}
}
while(switcher!=假);
System.out.println(“会话结束”);
}
私有静态void addPersonFriend(){
System.out.println(“\n遵循提示”);
newFriend=新的PersonalFriend(null,null,null,null,null,null,null,null,null,null,null,0,null);
System.out.print(“\n输入名字:”);
newFriend.setfName(sc.nextLine());
System.out.print(“输入姓氏:”);
newFriend.setlName(sc.nextLine());
系统输出打印(“输入地址:”);
setAddress(sc.nextLine());
系统输出打印(“输入城市:”;
newFriend.setCity(sc.nextLine());
系统输出打印(“进入状态:”);
newFriend.setState(sc.nextLine());
System.out.print(“输入Zip:”);
setZip(sc.nextLine());
系统输出打印(“输入电话号码:”);
setPhone(sc.nextLine());
系统输出打印(“输入手机号码:”);
setphone(sc.nextLine());
系统输出打印(“输入电子邮件号码:”);
setEmail(sc.nextLine());
系统输出打印(“输入生日:”;
newFriend.setBirthday(sc.nextLine());
系统输出打印(“输入年龄:”;
newFriend.setAge(sc.nextInt());
系统输出打印(“输入十二生肖:”);
newFriend.setZodiac(sc.nextLine());
系统输出打印(“”);
newFriend.setZodiac(sc.nextLine());
add(newFriend);
System.out.println(“\n您已成功添加了一个新人!”);
}
私有静态无效addBusinessPerson(){
System.out.println(“\n遵循提示”);
newBusiness=newbusinessassociate(null,null,null,null,null,null,null,null,null,null,null);
System.out.print(“\n输入名字:”);
newBusiness.setfName(sc.nextLine());
System.out.print(“输入姓氏:”);
newBusiness.setlName(sc.nextLine());
系统输出打印(“输入地址:”);
newBusiness.setAddress(sc.nextLine());
系统输出打印(“输入城市:”;
newBusiness.setCity(sc.nextLine());
系统输出打印(“输入St
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String name = get("First Name", "\\D+", scanner);

    }

    public static String get(String field, String regEx, Scanner scanner) {
        while (true) {
            System.out.println("Enter " + field + " :");
            String str = scanner.nextLine();
            if (str.matches(regEx)) {
                return str;
            }
            System.out.println("Please enter a correct " + field);
        }
    }
while (true)
{
    *Your code that will ask user to enter a value for address field.*
    *assign the entered value to the address field*
    *This loop will continue until correct value is received and assigned.*
}