2015-10-18 33 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