0

2つのファイルがあり、1(OrderCatalogue.java)が外部ファイルの内容を読み込み、2(以下)が読み込まれます。しかし、この行「OrderCatalogue catalogue = new OrderCatalogue();」に対して、「FileNotFoundExceptionをキャッチするか、スローするように宣言する必要があります」というエラーが発生します。それは方法ではないので、私はそれを理解しています。しかし、メソッドに入れようとすると、「getCodeIndex」メソッドと「checkOut」メソッドの下のコードは、「パッケージカタログが存在しません」というエラーメッセージで機能しません。コードを編集して機能させる方法を知っている人はいますか?ありがとうございました!!

public class Shopping {

OrderCatalogue catalogue= new OrderCatalogue();
ArrayList<Integer> orderqty = new ArrayList<>(); //Create array to store user's input of quantity
ArrayList<String> ordercode = new ArrayList<>(); //Create array to store user's input of order number

    public int getCodeIndex(String code)
    {    
        int index = -1;

        for (int i =0;i<catalogue.productList.size();i++)
        {            
            if(catalogue.productList.get(i).code.equals(code))
            {
            index = i;
            break;
            }
        }
        return index;
    }
    public void checkout()
    {
         DecimalFormat df = new DecimalFormat("0.00");
         System.out.println("Your order:");
         for(int j=0;j<ordercode.size();j++)
         {
            String orderc = ordercode.get(j);

            for (int i =0;i<catalogue.productList.size();i++)
            {
                if(catalogue.productList.get(i).code.equals(orderc))
                {
                    System.out.print(orderqty.get(j)+" ");
                    System.out.print(catalogue.productList.get(i).desc);
                    System.out.print(" @ $"+df.format(catalogue.productList.get(i).price)); 
                }
            }   
        }

    }

そしてこれは私のOrderCatalogueファイルです

public OrderCatalogue() throws FileNotFoundException

{

    //Open the file "Catalog.txt"
           FileReader fr = new FileReader("Catalog.txt");
           Scanner file = new Scanner(fr);


           while(file.hasNextLine())
           {
               //Read in the product details in the file
               String data = file.nextLine();
               String[] result = data.split("\\, "); 

               String code = result[0];
               String desc = result[1];
               String price = result[2];
               String unit = result[3];

               //Store the product details in a vector
               Product a = new Product(desc, code, price, unit);

               productList.add(a);
           }
4

1 に答える 1

2

OrderCatalogue コンストラクターが FileNotFoundException をスローするようです。Shopping コンストラクター内でカタログを初期化し、例外をキャッチするか、FileNotFoundException をスローするように宣言できます。

public Shopping() throws FileNotFoundException
{
        this.catalogue= new OrderCatalogue();

また

public Shopping()
    {
            try{
                this.catalogue= new OrderCatalogue();
            }catch(FileNotFoundException e)
                blah blah
            }
于 2013-02-16T04:32:02.293 に答える