英文:
Problems regarding a Java method to edit a txt file
问题
public static void Replace_Record(String editTerm, String newItem, String newAmount, String newPrice){
String filepath = "temp_Food_Item.txt";
String tempfile = "temp_Food_Item_temp.txt";
File oldFile = new File(filepath);
File newFile = new File(tempfile);
String item = "";
String quantity = "";
String price = "";
System.out.println("working");
try{
FileWriter fw = new FileWriter(tempfile, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
Scanner x = new Scanner(new File(filepath));
x.useDelimiter("[,/n]");
while(x.hasNext()){
item = x.next();
quantity = x.next();
price = x.next();
if(item.equalsIgnoreCase(editTerm)){
pw.println(newItem + "," + newAmount + "," + newPrice);
}
else{
pw.println(item + "," + quantity + "," + price);
}
}
x.close();
pw.flush();
pw.close();
oldFile.delete();
File dump = new File(filepath);
newFile.renameTo(dump);
}
catch(Exception e){
System.out.println("Error declared");
}
}
英文:
public static void Replace_Record(String editTerm, String newItem, String newAmount, String newPrice){
String filepath="temp_Food_Item.txt";
String tempfile= "temp_Food_Item_temp.txt";
File oldFile= new File(filepath);
File newFile=new File(tempfile);
String item=""; String quantity=""; String price="";
System.out.println("working ");
try{
//System.out.println("working pt1");
FileWriter fw= new FileWriter(tempfile,true);
BufferedWriter bw= new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
x = new Scanner(new File(filepath));
x.useDelimiter("[,/n]");
//System.out.println("working pt2");
while(x.hasNext()){
//System.out.println("working pt3");
item=x.next();
quantity=x.next();
price=x.next();
if(item.equalsIgnoreCase(editTerm)){
pw.println(newItem+","+newAmount+","+newPrice);
}
else{
//System.out.println("working pt4 ");
pw.println(item+","+quantity+","+price);
}
}
x.close();
pw.flush();
pw.close();
oldFile.delete();
File dump=new File(filepath);
newFile.renameTo(dump);
}
catch(Exception e){
System.out.println("Error declared");
}
}
I don't understand where I went wrong but it is printing "error declared" so I debugged and found after working pt1 it stops and goes to catch please help?
Additional info includes:
I am making a database for a restaurant and I am inputting info in txt files in the sequence item_name,item_amount,item_price so I am taking my new values from, main and passing them to the method, in theory, it first duplicates a file until it comes to the strings I wanna remove and then replaces them and goes back to copy the strings from the real files. but every time I run this I get catch.
TIA
答案1
得分: 2
虽然我不能立即回答你的问题,但我可以提供一些想法。
首先,捕获更明确的异常,比如IOException、FileNotFoundException。通常最好的做法是有更明确的代码,这是改进错误处理的第一步。
另外,针对它采取一些操作,起初你可以在控制台中打印它,并使用那些信息来调试你的程序。这可能会告诉你确切的错误及其位置。
英文:
While I can't answer your question straight away, I can offer a few ideas.
First of, catch a more explicit exception, such as IOException, FileNotFoundException. It is generally good practice to have more explicit code and it's the first step towards improved error handling.
Also do something with it, for startes you can print it in your console and use that information to debug your program. It might tell you exactly what your error is and where it is.
答案2
得分: 0
大家好,感谢你们在解决这个问题上给予的帮助,但我已经成功修复了它。我采纳了你们的建议,并运行了多种类型的异常处理,最终发现这是一个文件IO错误。我还遇到了关于文件命名的问题,因此编译器无法识别我调用的是哪个文件。除此之外,一切都很好,谢谢大家。
英文:
hello everyone thanks for helping me through this problem but I have managed to fix it I took your tips and ran multiple types of exception till I found this was a file io error and I had a problem about naming the files so the compiler could not recognize which file I was calling other than that we Gucci thank you guys
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论