Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布尔值无法返回正确的值_Java - Fatal编程技术网

Java 布尔值无法返回正确的值

Java 布尔值无法返回正确的值,java,Java,我有四个类别:顾客、商店、商品和在线 我正在使用“LinkedList”在课堂上移动 我在Customer类中添加了项目 当我想检查在线类下是否存在项时 因为你不能在客户处查看 我使用hasItem()。它经常失败 当我这么说的时候,我的意思是,即使我输入了“001A:GTA”,该项仍存储在数据中,并且不起作用。 它一直返回null 有人能给我解释一下吗? 我真的很感激。我是一个完全的初学者 项目类别 public class Item { private String code;

我有四个类别:顾客、商店、商品和在线

我正在使用“LinkedList”在课堂上移动

我在Customer类中添加了项目

当我想检查在线类下是否存在项时

因为你不能在客户处查看

我使用hasItem()。它经常失败

当我这么说的时候,我的意思是,即使我输入了“001A:GTA”,该项仍存储在数据中,并且不起作用。 它一直返回null

有人能给我解释一下吗? 我真的很感激。我是一个完全的初学者

项目类别

public class Item {
    private String code;
    private String name;
    public Item(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public boolean hasItem(String code, String name){
      return code.equals(this.code) && name.equals(this.name);
    }

    @Override
    public String toString() {
        return name + " : " + code;
    }

}
商店类

    public class Store {
    private LinkedList<Item> items = new LinkedList<Item>();

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

    public void addItem(String code, String name){
          items.add(new Item(this, code, name);
       }

    public LinkedList<Item> viewItem(){
           for(int j = 0 ; j < items.size(); j++)
               System.out.println(items.get(j))
           return null; 
    }
}
公共类存储{
私有LinkedList项=新建LinkedList();
公共存储(字符串名称、字符串编号){
this.name=名称;
这个数字=数字;
}
public void addItem(字符串代码、字符串名称){
项目。添加(新项目(此、代码、名称);
}
公共链接列表视图项(){
对于(int j=0;j
顾客

   public class Customer {
    private LinkedList<Store> stores = new LinkedList<Store>();

    public Customer() {
        stores.add(new Store("Game", "1"));
        stores.add(new Store("Grocery", "2"));

        stores.get(0).addItem("001A", "GTA");
        stores.get(0).addItem("001B", "GOD OF WARS");
        stores.get(0).addItem("001C", "THE LAST OF US");

        stores.get(1).addItem("002A", "Sandwich");
        stores.get(1).addItem("002B", "Cup Noodle");
        stores.get(1).addItem("002C", "Ice Cream");
    }

    public static void main(String args[]) {
        new Customer().view();
    }

    public void view() {
        System.out.println(stores.get(0).viewItem());
    }

}
公共类客户{
私有LinkedList存储=新建LinkedList();
公众客户(){
添加(新商店(“游戏”、“1”));
添加(新商店(“杂货店”、“2”));
存储。获取(0)。添加项(“001A”、“GTA”);
stores.get(0).addItem(“001B”,“战神”);
stores.get(0).addItem(“001C”,“我们的最后一个”);
商店。获取(1)。添加项(“002A”,“三明治”);
商店。获取(1)。添加项(“002B”,“杯面”);
商店。获取(1)。添加项(“002C”,“冰淇淋”);
}
公共静态void main(字符串参数[]){
新客户().view();
}
公共无效视图(){
System.out.println(stores.get(0.viewItem());
}
}
在线课程

class Online{
    private LinkedList<Item> items = new LinkedList<Item>();
    private String name ;
    private String number;
    public Online(String name, String number){
       this.name = name;
       this.number = number;
    }

   public static void main(String args[]){
          new Customer(“John”, “012”).view();
    }

   private void view(){
      Item item = item(“001A:GTA”);
      if(item != null)
         System.out.println(“Found”);
      else
        System.out.println(“Not found”):
   }
    public Item item(String item){
      String[] temp = item.split(":");
      String code = temp[0];
      String name = temp[1];
      for(Item item: items)
          if(item.hasItem(code, name))
             return item;
      return null;
    } 
   }
}
在线上课{
私有LinkedList项=新建LinkedList();
私有字符串名称;
私有字符串编号;
公共联机(字符串名称、字符串编号){
this.name=名称;
这个数字=数字;
}
公共静态void main(字符串参数[]){
新客户(“John”,“012”).view();
}
私有void视图(){
项目=项目(“001A:GTA”);
如果(项!=null)
System.out.println(“找到”);
其他的
System.out.println(“未找到”):
}
公共项目(字符串项目){
字符串[]temp=item.split(“:”);
字符串代码=temp[0];
字符串名称=temp[1];
用于(项目:项目)
if(项目。项目(代码、名称))
退货项目;
返回null;
} 
}
}

您正在搜索“001:GTA”,但在示例中,您存储了项“001A:GTA”

hasItem是类项的函数。但您似乎是针对项列表调用它

 if(items.hasItem(code, name))
我想应该是这样

 if(item.hasItem(code, name))
推翻 按如下方式操作:

import java.util.LinkedList;
import java.util.Objects;

class Item {
    private String code, name;

    public Item(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name + " : " + code;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, code);
    }

    @Override
    public boolean equals(Object obj) {
        Item item = (Item) obj;
        return name.equals(item.name) && code.equals(item.code);
    }
}

class Store {
    private LinkedList<Item> items = new LinkedList<Item>();
    private String name, number;

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

    public String getName() {
        return name;
    }

    public void addItem(String code, String name) {
        items.add(new Item(code, name));
    }

    public LinkedList<Item> findItems() {
        return items;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Item item : items) {
            sb.append(item.toString()).append(System.lineSeparator());
        }
        return sb.toString();
    }

    public void viewStore() {
        System.out.println(name + " " + number);
    }

    /**
     * Searches the item in the store using the search string
     * 
     * @param The string combining the code and name in the format "code:name:"
     * @return Returns the matching Item. If the item is not found, null is returned
     */
    public Item findItem(String strItem) {
        String[] data = strItem.split(":");
        if (data.length < 2) {
            return null;
        }

        Item searchItem = new Item(data[0], data[1]);

        for (Item item : items) {
            if (item.equals(searchItem)) {
                return item;
            }
        }

        return null;
    }

    /**
     * Searches the item in the store using the name of the item
     * 
     * @param The name of the item
     * @return Returns the matching Item. If the item is not found, null is returned
     */
    public Item findItemByName(String itemName) {
        for (Item item : items) {
            if (item.getName().equals(itemName)) {
                return item;
            }
        }
        return null;
    }
}

class Customer {
    private LinkedList<Store> stores = new LinkedList<Store>();

    public Customer() {
        stores.add(new Store("Game", "1"));
        stores.add(new Store("Grocery", "2"));

        stores.get(0).addItem("001A", "GTA");
        stores.get(0).addItem("001B", "GOD OF WARS");
        stores.get(0).addItem("001C", "THE LAST OF US");

        stores.get(1).addItem("002A", "Sandwich");
        stores.get(1).addItem("002B", "Cup Noodle");
        stores.get(1).addItem("002C", "Ice Cream");
    }

    public LinkedList<Store> getStores() {
        return stores;
    }

    public void viewStore() {
        for (Store store : stores)
            store.viewStore();
    }

    public void viewItems() {
        for (Store store : stores) {
            for (Item item : store.findItems()) {
                System.out.println(item);
            }
        }
    }

    /**
     * Searches the item in all the stores related to the customer using the search
     * string
     * 
     * @param The string combining the code and name in the format "code:name:"
     * @return Returns the matching Item. If the item is not found, null is returned
     */
    public Item findItem(String strItem) {
        String[] data = strItem.split(":");
        if (data.length < 2) {
            return null;
        }

        Item searchItem = new Item(data[0], data[1]);

        for (Store store : stores) {
            for (Item item : store.findItems()) {
                if (item.equals(searchItem)) {
                    return item;
                }
            }
        }
        return null;
    }

    /**
     * Searches the item in all the stores related to the customer using the name of
     * the item
     * 
     * @param The name of the item
     * @return Returns the matching Item. If the item is not found, null is returned
     */
    public Item findItemByName(String itemName) {
        for (Store store : stores) {
            for (Item item : store.findItems()) {
                if (item.getName().equals(itemName)) {
                    return item;
                }
            }
        }
        return null;
    }
}

public class Online {
    public static void main(String[] args) {
        Item item;
        String strItem, itemName;
        Customer customer = new Customer();

        // Test searching the item using the search string in a particular store e.g.
        // first store of customer
        Store store = customer.getStores().get(0);
        System.out.println("Searching item using search string in store, " + store.getName());

        strItem = "001A:GTA";
        item = store.findItem(strItem);
        if (item == null) {
            System.out.println("Not found -> " + strItem);
        } else {
            System.out.println("Found -> " + item);
        }

        strItem = "002A:Sandwich";
        item = store.findItem(strItem);
        if (item == null) {
            System.out.println("Not found -> " + strItem);
        } else {
            System.out.println("Found -> " + item);
        }

        System.out.println();

        // Test searching the item using the search string in all the stores related to
        // the customer
        System.out.println("Searching item using search string in all the stores related to the customer");
        strItem = "001A:GTA";
        item = customer.findItem(strItem);
        if (item == null) {
            System.out.println("Not found -> " + strItem);
        } else {
            System.out.println("Found -> " + item);
        }

        strItem = "002A:Sandwich";
        item = customer.findItem(strItem);
        if (item == null) {
            System.out.println("Not found -> " + strItem);
        } else {
            System.out.println("Found -> " + item);
        }

        strItem = "ABC:XYZ";
        item = customer.findItem(strItem);
        if (item == null) {
            System.out.println("Not found -> " + strItem);
        } else {
            System.out.println("Found -> " + item);
        }

        System.out.println();

        // Test searching the item using name of the item in a particular store e.g.
        // first store of customer
        store = customer.getStores().get(0);
        System.out.println("Searching item using name of the item in store, " + store.getName());

        itemName = "GTA";
        item = store.findItemByName(itemName);
        if (item == null) {
            System.out.println("Not found -> " + strItem);
        } else {
            System.out.println("Found -> " + item);
        }

        itemName = "Sandwich";
        item = store.findItemByName(itemName);
        if (item == null) {
            System.out.println("Not found -> " + itemName);
        } else {
            System.out.println("Found -> " + item);
        }

        System.out.println();

        // Test searching the item using the name of the item in all the stores related
        // to the customer
        System.out.println("Searching item  using name of the item in all the stores related to the customer");
        itemName = "GTA";
        item = customer.findItemByName(itemName);
        if (item == null) {
            System.out.println("Not found -> " + itemName);
        } else {
            System.out.println("Found -> " + item);
        }

        itemName = "Sandwich";
        item = customer.findItemByName(itemName);
        if (item == null) {
            System.out.println("Not found -> " + itemName);
        } else {
            System.out.println("Found -> " + item);
        }

        itemName = "XYZ";
        item = customer.findItemByName(itemName);
        if (item == null) {
            System.out.println("Not found -> " + itemName);
        } else {
            System.out.println("Found -> " + item);
        }
    }
}

请注意,
Item
类有一个名为,
hasItem
的方法听起来不太好。它给人的印象好像类,
Item
是一个容器类。另外,建议在类中重写
Object::equals
Object::hashCode
。这样可以使代码干净(正如您在上面给出的代码中所看到的)并帮助您以简单的方式编写逻辑;尤其是在处理集合时。

您的Item类不应具有名为
hasItem()的方法
。物品没有物品。
商店
类应该有这种方法。但是商店没有连接到“在线”你似乎对同一个项目问了很多问题,涉及
客户
商店
,和
物品
。我认为你应该学习。你的
hasItem
应该被称为
equalTo吗?我认为它应该是指
contentEquals
,而不是
equals
。不是@Jeff Holt。当我刚刚键入GTA时,我得到的数组索引超出了执行范围。我是否应该将其设为null并忽略它@Arvind。您需要检查结果数组的大小(由拆分产生)在尝试从中访问元素之前。我已经以
public Item getItem(String strItem){String[]data=strItem.split(“:”);if(data.length<2){return null;}Item searchItem=new Item(data[0],data[1]);/…}
的形式完成了此操作。您可以看到我已经选中了
if(数据长度<2)
。你应该经常做这样的检查。如果有任何进一步的疑问/问题,请随时发表评论。哦,对不起,我忘了解释我想做什么。如果strTerm只是“GTA”,我想自动输出他们拥有的项目。非常感谢@arvind这是标准方式,我建议你坚持标准。如果你愿意,你可以发布一个针对这一要求的单独问题,
是否有任何方法可以在不使用@Override Objects::equal的情况下等于两个项目。但是,我不建议这样做,因为人们可能会否决这个问题,因为每个人都建议坚持标准。如果幸运的话,你会得到答案,甚至会对这个问题投赞成票。
Searching item using search string in store, Game
Found -> GTA : 001A
Not found -> 002A:Sandwich

Searching item using search string in all the stores related to the customer
Found -> GTA : 001A
Found -> Sandwich : 002A
Not found -> ABC:XYZ

Searching item using name of the item in store, Game
Found -> GTA : 001A
Not found -> Sandwich

Searching item  using name of the item in all the stores related to the customer
Found -> GTA : 001A
Found -> Sandwich : 002A
Not found -> XYZ