Java FileNotFoundException error as statement not in a method -
i have 2 files 1(ordercatalogue.java) reads in contents of external file , 2(below). i'm having "filenotfoundexception must caught or declard thrown" error line "ordercatalogue catalogue= new ordercatalogue();" , understand because not in method. if seek puting in method, code under "getcodeindex" , "checkout" methods can't work error message of "package catalogue not exist". has thought how can edit code create them work? give thanks you!!
public class shopping { ordercatalogue catalogue= new ordercatalogue(); arraylist<integer> orderqty = new arraylist<>(); //create array store user's input of quantity arraylist<string> ordercode = new arraylist<>(); //create array store user's input of order number public int getcodeindex(string code) { int index = -1; (int =0;i<catalogue.productlist.size();i++) { if(catalogue.productlist.get(i).code.equals(code)) { index = i; break; } } homecoming 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); (int =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)); } } } }
and ordercatalogue file
public ordercatalogue() throws filenotfoundexception { //open file "catalog.txt" filereader fr = new filereader("catalog.txt"); scanner file = new scanner(fr); while(file.hasnextline()) { //read in product details in file string info = file.nextline(); string[] result = data.split("\\, "); string code = result[0]; string desc = result[1]; string cost = result[2]; string unit = result[3]; //store product details in vector product = new product(desc, code, price, unit); productlist.add(a); }
it seems ordercatalogue constructor throws filenotfoundexception. can initialize catalogue within shopping constructor , grab exception or declare throw filenotfoundexception.
public shopping() throws filenotfoundexception { this.catalogue= new ordercatalogue();
or
public shopping() { try{ this.catalogue= new ordercatalogue(); }catch(filenotfoundexception e) blah blah }
java
No comments:
Post a Comment