2015-10-18 29 views
0

我有一個類型爲Store的列表,用戶可以在列表中添加項目,其中包含與它們相關聯的名稱和ID。在列表中搜索提供了錯誤的結果

public class StoreSearch { 

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

     ArrayList <Store> stores = new ArrayList();  

     String input = ""; 
     String name; 
     int id = 0; 
     int newId = 0; 
     int index = 0; 

     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

     while(!(input.equals("quit"))) { 
      System.out.println("Hello!\nEnter add or search"); 
      input = in.readLine(); 
      if(input.equalsIgnoreCase("add")) { 
       System.out.println("Enter a name "); 
       name = in.readLine(); 

       System.out.println("Enter a id"); 
       input = in.readLine(); 
       id = Integer.parseInt(input); 

       Store s = new Store(name,id); 

        if(!stores.contains(s)) 
         stores.add(s);//only add if combination of name and id are not in it 
       } 

      if(input.equals("search")) { 

       System.out.println("Enter a name"); 
       name = in.readLine(); 

       System.out.println("Enter a id guideline"); 
       input = in.readLine(); 
       index = input.indexOf("-"); 

       if(index == 0) { 
        String substring = input.substring(input.lastIndexOf("-") + 1); 
        newId = Integer.parseInt(substring); 
        Store s = new Store(name,id); 

        for(int counter = 0; counter < stores.size(); counter++) { 
         if(stores.contains(s)) { 
          System.out.println(stores.toString()); 
         } 
        } 

       } 

       if(index == 4) { 
        String[] parts = input.split("\\-"); // String array, each element is text between dots 
        newId = Integer.parseInt(parts[0]); 
        //the hyphen after the 4 digit number 
       } 
       else { 
        //only id 

       } 


      } 
     } 

     } 
    } 

和存儲類:

public class Store { 
    private String name; 
    private int id; 

public Store(String name, int id) { 
    this.name = name; 
    this.id = id; 
} 

@Override 
    public String toString() { 
     return " Name " + name + " id " + id; 
    } 

@Override 
    public boolean equals(Object obj) { 

     if(obj instanceof Store){ 
      Store element = (Store) obj; 
      if(this.name.equals(element.name) && element.id == (this.id)){ 
       return true; 
      } 
     } 
     return false; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 7; 
     hash = 61 * hash + Objects.hashCode(this.name); 
     hash = 61 * hash + this.id; 
     return hash; 
    } 
} 

我有添加到列表中,在那裏我如果的事的組合進入我只添加到列表中沒有任何問題,它的名稱和ID不存在已經存在。然而,我試圖搜索列表,這導致了我的問題。

舉例來說,如果我已經添加了這些元素的列表:

Snack 3366 
Apple 3367 
Apple 3368 

,我想搜索列表如下:

名稱爲「蘋果」 標識準則是本"-3368"意義,應該打印出任何具有相同名稱並具有3368以前的對象的對象。但是,我的輸出從來不會這樣做。我嘗試使用stores.get(index);打印出來,但仍然給我錯誤的輸出。

對於第二條if語句,它檢查它們是否是4位數字後面的連字符,在這種情況下,「3370-」意味着所有輸入名稱的對象,並且應該返回id 3370及以上。考慮到我無法弄清楚第一個陳述,我無法嘗試第二個陳述。任何幫助,將不勝感激。

+0

'如果(OBJ的instanceof書){'?你什麼時候到要比較一個'Book'反對'Store'?另外,你可以使用'Set'而不是'List'來保證不安全 – MadProgrammer

+0

對不起,我只是修復了它,我也被迫使用了arrayL爲此。 – user3739406

+0

爲什麼if(stores.contains(s)){'在for循環中?你不使用'counter'。 –

回答

1

所以,這...

index = input.indexOf("-"); 

if(index == 0) { 
    String substring = input.substring(input.lastIndexOf("-") + 1); 
    newId = Integer.parseInt(substring); 
    Store s = new Store(name,id); 

沒有意義,因爲它假定-是第一個字符,我想你的意思是使用if(index >= 0) {

此外,

System.out.println("Enter a id guideline"); 
input = in.readLine(); 
input = in.readLine(); 
index = input.indexOf("-"); 

雙讀也似乎很奇怪

敲響了一下之後,我想你想要更多的東西一樣......

System.out.println("Enter a name"); 
name = in.readLine(); 

System.out.println("Enter a id guideline"); 
input = in.readLine(); 
index = input.indexOf("-"); 

try { 
    String currentIDValue = input; 
    String replaceIDValue = null; 
    id = 0; 
    if (index >= 0) { 
     currentIDValue = input.substring(0, input.lastIndexOf("-")); 
     replaceIDValue = input.substring(input.lastIndexOf("-") + 1); 

     id = Integer.parseInt(currentIDValue); 
    } else { 
     id = Integer.parseInt(currentIDValue); 
    } 

    Store s = new Store(name, id); 
    if (stores.contains(s)) { 

     index = stores.indexOf(s); 
     s = stores.get(index); 

     System.out.println("You have selected " + s); 
     if (replaceIDValue != null) { 

      newId = Integer.parseInt(replaceIDValue); 
      // update the ID 

     } 

    } else { 

     System.out.println("Item does not exist"); 
    } 
} catch (NumberFormatException exp) { 
    exp.printStackTrace(); 
} 

現在,這種讀取用戶的輸入,它檢查-並採取適當的行動,現在我已經包含了能力執行只是一個搜索,以及搜索和更新

看來,無論我把什麼名字時,我有添加到列表中已經,當我搜索至少兩個對象,我總是這兩個對象輸出兩次(2行)。如果我列表中的橙色2222和列表中的橙色2223,並用「-2222」搜索橙色名稱,我會在兩行中得到第一個結果,第二個元素也會顯示出來,儘管我在問數字爲2222及以下。

這是因爲您的原始代碼完全按照您所說的做了......

for(int counter = 0; counter < stores.size(); counter++) { 
    if(stores.contains(s)) { 
     System.out.println(stores.toString()); 
    } 
} 

List每個項目,把它打印出來,但前提是List包含s,所以假設s匹配清單中的項目中的任何一個,將打印的所有項目。

的迴路不是必需的,你可以簡單地使用的List#containsList#indexOf

組合「-2222」是2222新的ID,和連字符是所有以前的ID名稱相同。 「2222-」2222又是新的id,hypen是更大的id(大於2222同名)「。

好吧,所以我們不只是搜索一個單一的項目,但一系列符合規定條件的項目,所以像...的

if (input.equals("search")) { 

     System.out.println("Enter a name"); 
     name = in.readLine(); 

     System.out.println("Enter a id guideline"); 
     input = in.readLine(); 

     String parts[] = input.split("-"); 

     int lower = 0; 
     int upper = 0; 

     if (parts.length >= 1 && parts.length <= 2) { 
      if (parts.length == 2) { 
       if (parts[0] != null && parts[0].trim().length() > 0) { 
        // x-x 
        lower = Integer.parseInt(parts[0]); 
        upper = Integer.parseInt(parts[1]); 
       } else { 
        // -x 
        lower = Integer.MIN_VALUE; 
        upper = Integer.parseInt(parts[1]); 
       } 
      } else if (parts.length == 1) { 
       // x- 
       lower = Integer.parseInt(parts[0]); 
       upper = Integer.MAX_VALUE; 
      } 
      for (Store store : stores) { 
       if (store.id >= lower && store.id <= upper && store.name.equals(name)) { 
        System.out.println(store); 
       } 
      } 
     } else { 
      System.out.println("Invalid input"); 
     } 

    } 
} 

可能更合適

+0

雙重閱讀已經修復,但即使看起來改變了第一個,仍然給我同樣的問題。當我在列表中至少添加了兩個對象時,無論我輸入什麼名稱,當我搜索時,我總是將兩個對象輸出兩次(2行)。如果我在列表中有** Orange 2222 **和列表中有** Orange 2223 **,並且搜索帶有「-2222」的名稱Orange,我在兩條不同的線上得到第一個結果,第二個元素也是顯示,即使我要求2222及以下的號碼。 – user3739406

+0

首先,我要拋棄循環,'List#indexOf'就是你真正需要找到你正在搜索的項目的位置(或者如果它不存在,則爲'-1')。沒有退出循環(其他讀取'List'中的所有項目),因此它會打印所有項目。我更新了一部分基於我「想」你想要做的事情,看看是否對你更有意義;) – MadProgrammer

+0

嗯只是試過了代碼,得到了一個數字格式異常。我會稍微接受你的答案,我只是想讓這個工作。 – user3739406