java - 无法找出循环。希望得到帮助

标签 java exception loops bluej

我对 Java 还很陌生,我正在使用 BlueJ。为了使我的程序正常运行,我需要:

    //  Insert code here to perform a sequence of
    //  interactive transactions with the user.
    //  The user enters an item number and the program
    //  either displays the item or prints an error message
    //  if the item is not found.  The program terminates
    //  when the user enters zero as the item number.

不幸的是,我似乎无法让该程序运行。希望有人能帮助我。

这是需要循环的类Program2:

import java.util.*;

public class Program2 {
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        Catalog store = new Catalog(3);
        int itemnum = 0;
        Item item;

        try {
            store.insert
              (new Music(1111, "Gold", 12.00, "Abba"));
            store.insert
              (new Movie(2222, "Mamma Mia", 16.00, "Meryl Streep"));
            store.insert
              (new Book(3333, "DaVinci Code", 8.00, "Dan Brown"));
              store.insert
            (new Music(4444, "Legend", 15.00, "Bob Marley"));
            } catch (CatalogFull exc) {
                System.out.println(exc);
            }


        //  Insert code here to perform a sequence of
        //  interactive transactions with the user.
        //  The user enters an item number and the program
        //  either displays the item or prints an error message
        //  if the item is not found.  The program terminates
        //  when the user enters zero as the item number.

        itemnum = kbd.nextInt();
        while (itemnum == 0) {
            item = store.find(itemnum);
            if (item != null) {
                System.out.print(itemnum);
            } else {
                System.out.printf("%s was not found.%n", item);
            }
            System.out.println();
            System.out.print("Player (0 to exit)? ");
            itemnum = kbd.nextInt();
        }
    }
}

目前,当前错误位于“(itemnum)”循环中:

unreported exception ItemNotFound; must be caught or declared to be thrown

话虽这么说,我怀疑这不是唯一的问题,而且我没有正确执行程序的这一部分。希望有人可以帮助我。预先感谢您。

以下是一些可能有用或可能没用的其他类:

这是类ItemNotFound:

// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
    public ItemNotFound(int id) {
        super(String.format("Item %d was not found.", id));
    }
}

这是类CatalogFull

// This exception is thrown when trying to insert an item
// when the catalog is full.
public class CatalogFull extends Exception {
    public CatalogFull() {
        super("The catalog is full.");
    }
}

这是类目录:

public class Catalog {
    private Item[] list;
    private int size;

    // Construct an empty catalog with the specified capacity.
    public Catalog(int max) {
        list = new Item[max];
        size = 0;
    }

    // Insert a new item into the catalog.
    // Throw a CatalogFull exception if the catalog is full.
    public void insert(Item obj) throws CatalogFull {
        if (list.length == size) {
            throw new CatalogFull();
        }
        list[size] = obj;
        ++size;
    }

    // Search the catalog for the item whose item number
    // is the parameter id.  Return the matching object 
    // if the search succeeds.  Throw an ItemNotFound
    // exception if the search fails.
    public Item find(int id) throws ItemNotFound {
        for (int pos = 0; pos < size; ++pos){
            if (id == list[pos].getItemNumber()){
                return list[pos];
            }
        }
        throw new ItemNotFound(id);
        }
}

这是类Item:

public class Item {
    private int itemnum;
    private String title;
    private double price;

    // Construct a new item object.
    public Item(int id, String t, double p) {
        itemnum = id;
        title = t;
        price = p;
    }

    // Return the item number of this item.
    public int getItemNumber() {
        return itemnum;
    }

    // Return the item type. This is overridden in subclasses.
    public String getItemType() {
        return "Item";
    }

    // Return a printable String represenation of this item.
    public String toString() {
        String line1, line2, line3, line4, out;
        String itemtype = this.getItemType();
        line1 = String.format("Item number: %d%n", itemnum);
        line2 = String.format("Item type:   %s%n", itemtype);
        line3 = String.format("Item title:  %s%n", title);
        line4 = String.format("Item price:  %.2f%n", price);
        out = line1 + line2 + line3 + line4;
        return out;
    }
}

最佳答案

item = store.find(itemnum); //here you are finding the item 

您的 find(int id) 方法引发 ItemNotFound 异常。我们需要在这里处理 ItemNotFound 异常。

    itemnum = kbd.nextInt();
    while (itemnum == 0) {
      try{
        item = store.find(itemnum);

          if (item != null) {
             System.out.print(itemnum);
          } 
        } catch (ItemNotFound e){
            System.out.println(" Item  was not found with id :"+ itemnum);
        }
        System.out.println();
        System.out.print("Player (0 to exit)? ");
        itemnum = kbd.nextInt();
    }

关于java - 无法找出循环。希望得到帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19152080/

相关文章:

java - 显示网址剪贴板

java - 我想将某些列的边框设置为粗,但所有列都已设置

java - 当我尝试在 PrintWriter 上写入时,为什么会抛出异常?

r - 使用新数据集在 purrr::map 之后预测 glm 模型的概率

linux - 创建 bash 脚本以自动执行分析多个文件的任务

java - 如何在Windows中使用cmd将war文件解压到新文件夹(或另一个驱动器)

java - 线程中的异常 "main"org.hibernate.MappingException : Association references unmapped class hibernate

php - 如何在 Laravel 中手动返回或抛出验证错误/异常?

python - 高效的双for循环

java - 在 SVNKit 中查询