2015-04-04 58 views
-2

我在过去的一周尝试解决如何检查Id是否已添加到数组中,并且如果有,请阻止用户输入其余数据,我非常感谢一些帮助。在对象数组上检查ID

我的代码:

class CustomerUse { 
    public static int checkId(Customer theArray[], int noOfValues, 
     String searchId) { 
     int idCheck = 0; 
     int step = 0; 
     while (step < noOfValues) { 
     step++; 
     if ((step < noOfValues) && (theArray[step].getId().equals(searchId))) { 
      idCheck = -1; 
     } 
     } 
     return idCheck; 
    } 

    public static void listAllNames(Customer[] names, int NoOfElements) { 
     int index; 
     String result; 
     for (index = 0; index < NoOfElements; index++) { 
     System.out.println(names[index]); 
     } 
    } 

    public static int findPlace(Customer theArray[], int balance, int noOfValues) { 
     int step; 
     int place; 
     step = 0; 
     while ((step < noOfValues) && (balance < theArray[step].getBalance())) { 
     step++; 
     } 
     place = step; 
     return place; 
    } 

    public static int addOne(Customer theArray[], int place, String Id, 
     Customer theObject, int noOfValues) { 
     int step; 
     if (noOfValues == 0) { 
     theArray[0] = theObject; 
     noOfValues++; 
     } else { 
     for (step = noOfValues - 1; step >= place; step--) { 
      theArray[step + 1] = theArray[step]; 
     } 
     theArray[place] = theObject; 
     noOfValues++; 
     } 
     return noOfValues; 
    } 

    public static int deleteName(Customer theArray[], int noOfElements) { 
     String searchId; 
     int step; 
     int whichOne = 0; 
     System.out.println("Enter Id of customer to be deleted "); 
     searchId = EasyIn.getString(); 
     step = 0; 
     while ((step < noOfElements) 
      && !(theArray[step].getId().equals(searchId))) { 
     step++; 
     } 
     if (step < noOfElements) { 
     whichOne = step; 
     for (step = whichOne; step < noOfElements - 1; step++) { 
      theArray[step] = theArray[step + 1]; 
     } 
     noOfElements--; 
     } else { 
     System.out.println(" Sorry this customer doesn't exist"); 
     } 
     return noOfElements; 
    } 

    public static void main(String[] args) { 
     Customer empArray[]; 
     int index; 
     int noOfElements; 
     String searchName; 
     Customer tempObject; 
     int step; 
     int option = 0; 
     int place; 
     String newName = ""; 
     String newId; 
     int newBalance; 
     int checkID = 0; 
     empArray = new Customer[100000]; 
     noOfElements = 0; 
     System.out 
      .println("\n 1. Add Customer \n 2. Delete Customer \n 3. List all Customers \n 4. Exit"); 
     System.out.print("Enter Option "); 
     option = EasyIn.getInt(); 
     while (option != 4) { 
     if (option == 1) { 
      System.out.print("Enter ID "); 
      newId = EasyIn.getString(); 
      System.out.print("Enter name "); 
      newName = EasyIn.getString(); 
      System.out.print("Enter Balance "); 
      newBalance = EasyIn.getInt(); 
      System.out.println(); 
      tempObject = new Customer(newName, newId, newBalance); 
      place = findPlace(empArray, newBalance, noOfElements); 
      noOfElements = addOne(empArray, place, newId, tempObject, 
        noOfElements); 
      checkID = checkId(empArray, noOfElements, newId); 
      if (checkID == -1) { 
       System.out.println("Sorry this ID already exists"); 
       checkID = 0; 
      } 
     } else if (option == 2) { 
      noOfElements = deleteName(empArray, noOfElements); 
     } else if (option == 3) { 
      listAllNames(empArray, noOfElements); 
     } 
     System.out.print("Enter Option "); 
     option = EasyIn.getInt(); 
     } 
    } 
} 
+0

请将您的代码降至“最低限度”,以显示您的问题并在此处发布。否则,您可能会收到比有用答案更多的降价。 – GhostCat 2015-04-04 13:25:28

回答

1

这有效地跳过数组项索引0:

while (step < noOfValues) 
{ 

    step++; 

更好:

public static int checkId(Customer theArray[], int noOfValues, String searchId){ 
    for (int step = 0; step < noOfValues; ++step){ 
     if(theArray[step].getId().equals(searchId)) 
     { 
      return step; 
     } 
    } 
    return -1; 
} 

返回-1上没有找到,否则该索引。

我还没有检查你的代码的其余部分,只是看到应该放弃使用数组而不是使用List。

+0

非常感谢Laune,在稍微编辑了你给定的代码后,我设法让它工作。谢谢您的帮助。也感谢Hovercraft的建议。 – LPG 2015-04-04 14:03:21