从数组字符串中删除用户副本

时间:2015-10-09 03:17:32

标签: java arrays string boolean duplicates

我正在尝试创建一个程序,接受用户想要输入的社会安全号码。唯一的限制是数字必须遵循格式XXX-XX-XXXX,不接受重复的条目。这就是我到目前为止所做的:

子类2

JFRAME

子类1

package SSNServerStorageExpanded;

class SSNArray{
    final String[] ssnNumber;
    int arrayCount;

    public SSNArray(){//defult contructor
        ssnNumber = new String[9999];
        arrayCount = 0;
    }

    public SSNArray(int arraySize){
        ssnNumber = new String[arraySize];
        arrayCount = 0;
    }

    public String[] getSSNNumber(){
        return ssnNumber;
    }

    public int getArrayCount(){
        return arrayCount;
    }

    public boolean validateSSNNumber(String SSNFormat){
        return SSNFormat.matches("\\d{3}-\\d{2}-\\d{4}");
    }

    public String addSSN(String SSNFormat){
        if(validateSSNNumber(SSNFormat)){
            return ssnNumber[arrayCount++] = SSNFormat;
        }else{
            return null;
        }
    }

    @Override
    public String toString(){
        String str = "\nThe Social Security Number(s) you entered is(are):\n";
        for(int x = 0; x < arrayCount; x++){/
            str += ssnNumber[x] + "\n";
        }return str;
    }
}

Mainclass

package SSNServerStorageExpanded;

public class SSNArrayExpanded extends SSNArray{

    public SSNArrayExpanded(){
        super();
    }

    public SSNArrayExpanded(int arraySize){
        super(arraySize);
    }

    @Override
    public boolean validateSSNNumber(String SSNFormat){
        if(super.validateSSNNumber(SSNFormat)){
            boolean duplicate = false;
            for(int y = 0; y < arrayCount; y++){
                if(ssnNumber[y].equals(ssnNumber[arrayCount])){
                    System.out.println("No duplicates allowed, please try again");
                    duplicate = true;
                    break;
                }
            }
            if(!duplicate){
                arrayCount++;
            }
        } 
        return true;
    }   
}

我在Subclass 1下使用 public boolean validateSSNNumber 方法做错了什么,或者我的代码中有更多错误,我不知道?

5 个答案:

答案 0 :(得分:0)

在你的子类1中你可以试试这个。您需要将用户输入的SSNFormat字符串与数组值进行比较(您正在比较数组值本身)。不要在这里增加数组计数,而是像你一样在addSSN函数中执行它。

 @Override
public boolean validateSSNNumber(String SSNFormat){
    if(super.validateSSNNumber(SSNFormat)){
        boolean duplicate = false;
        for(int y = 0; y < arrayCount; y++){
            if(ssnNumber[y].equals(SSNFormat)){
                System.out.println("No duplicates allowed, please try again");
                duplicate = true;
                break;
            }
        }
        if(!duplicate){
            return true;
        }
    } 
    return false;
} 

在SSNArray类中,使用此函数添加SSN编号,而无需再次验证SSNFormat。

 public String addSSN(String SSNFormat){
        return ssnNumber[arrayCount++] = SSNFormat;
    }

答案 1 :(得分:0)

您可以尝试使用 Set ,它可以轻松帮助您检查任何重复项,并减少您对阵列的迭代。

答案 2 :(得分:0)

这是您的完整解决方案,您可以根据需要添加N个号码的SSN号码,

import java.util.HashSet;
import java.util.Scanner;


class SSNSet{
    final HashSet<String> allSsnNumber = new HashSet<String>();

    public HashSet<String> getAllSsnNumber() {
        return allSsnNumber;
    }

    public boolean validateSSNNumber(String SSNFormat){
        return SSNFormat.matches("\\d{3}-\\d{2}-\\d{4}");
    }

    public boolean addSSN(String SSNFormat){
        if(validateSSNNumber(SSNFormat)){
            boolean flag;
            if(allSsnNumber.add(SSNFormat)){
                System.out.println("Added Successfully");
                flag = true;
            }else{
                System.out.println("Duplicate Not Allow");
                flag = false;
            }
            return flag;
        }else{
            System.out.println("!Please use the format XXX-XX-XXXX!");
            return false;
        }
    }

}

public class SSNArrayTestExpanded{
    public static void main(String[] args){
        SSNSet SSNArrayExpandedObject = new SSNSet();
        Scanner input = new Scanner(System.in);

        System.out.println("Initiating SSN Server Storage Expanded");
        System.out.println(" Type 'EXIT' at any moment to close the program \n");

        boolean run = true;
        while(run){
            System.out.print("Enter your Social Security Number(XXX-XX-XXXX): ");
            String ssnNumber = input.next();

            if(ssnNumber.equalsIgnoreCase("EXIT")){
                break;
                /*System.out.print(SSNArrayExpandedObject.validateSSNNumber(ssnNumber));
                return;*/
            }else{
                SSNArrayExpandedObject.addSSN(ssnNumber);
            }
        }

        System.out.println("===============================================");
        System.out.println("You have entered SSN Numbers are : ");
        System.out.println(SSNArrayExpandedObject.getAllSsnNumber());
        System.out.println("===============================================");
        System.out.println("Program Ended Successfully");
    }
}

和输出是:

Initiating SSN Server Storage Expanded
 Type 'EXIT' at any moment to close the program 

Enter your Social Security Number(XXX-XX-XXXX): 111-11-1111
Added Successfully
Enter your Social Security Number(XXX-XX-XXXX): 222-22-222
!Please use the format XXX-XX-XXXX!
Enter your Social Security Number(XXX-XX-XXXX): 111-11-1111
Duplicate Not Allow
Enter your Social Security Number(XXX-XX-XXXX): 333-33-333
!Please use the format XXX-XX-XXXX!
Enter your Social Security Number(XXX-XX-XXXX): 333-33-3333
Added Successfully
Enter your Social Security Number(XXX-XX-XXXX): EXIT
===============================================
You have entered SSN Numbers are : 
[111-11-1111, 333-33-3333]
===============================================
Program Ended Successfully

答案 3 :(得分:0)

为什么不能使用LinkedHashSet数据结构来存储ssn编号。?,提供简单的检索和重复检查,顺序为O(1)。

 final LinkedHashSet<String> ssnNumber;

也是代码

@Override
public boolean validateSSNNumber(String SSNFormat){
if(super.validateSSNNumber(SSNFormat)){
    boolean duplicate = ssnNumber.add(SSNFormat);
    if(duplicate){
       System.out.println("No duplicates allowed, please try again");
       return false;
     }
     return true;
   }
   return false    
} 

答案 4 :(得分:0)

    public boolean validateSSNNumber(String SSNFormat) {
            if (super.validateSSNNumber(SSNFormat)) {
               /*
                *Hashset add() - Returns true if this set did not already contain 
                *the specified element.
                *If this set already contains the element, the call leaves the set
                * unchanged and returns
                */
                boolean duplicate = !uniqueSSNNum.add(SSNFormat);
                return duplicate;
            }
            return false;
        }

    //To use HastSet it's better if you override equals and hashcode 
    //using the fields that you'll use for comparison equality
    // generated using eclipse
    @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + arrayCount;
            result = prime * result + Arrays.hashCode(ssnNumber);
            return result;
        }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SSNArray other = (SSNArray) obj;
        if (arrayCount != other.arrayCount)
            return false;
        if (!Arrays.equals(ssnNumber, other.ssnNumber))
            return false;
        return true;
    }