简体   繁体   English

写入文本文件时出现故障

[英]Glitch when writing to a text file

I am trying to develop a program for a school project. 我正在尝试为学校项目开发程序。 However I am not very experienced with coding, so naturally I run into frequent problems. 但是我对编码不是很有经验,所以自然会遇到很多问题。 The whole premise of my project is quite simple, I am creating a Horse Management System, which is basically just fancy wording for a database. 我的项目的整个前提非常简单,我正在创建一个Horse Management System,该系统基本上只是数据库的花哨措辞。 Firstly I add information about the horse, such as it's Name, Breed, DOB and other basic information. 首先,我添加有关马匹的信息,例如名称,品种,DOB和其他基本信息。 After that a horse profile has been created. 之后,创建了一个马配置文件。 Now you can add information such as it's training schedule, nutritional information and it's past injuries. 现在,您可以添加信息,例如训练时间表,营养信息以及过去的受伤情况。 Today while trying to add information for it's nutrition, I ran into a problem where it just outputs a bunch of random text. 今天,在尝试添加营养信息时,我遇到了一个问题,即它只能输出一堆随机文本。 Here's what is written on the text file when I try to write to it . 这是我尝试写入文本文件时写入的内容 I also have a section where you have to pull the data from the file and thus making it editable, but without this section done I can't do anything. 我也有一个部分,您必须从文件中提取数据并使其可编辑,但是如果没有完成此部分,我将无能为力。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
boolean entryError = false; 
if(FoodName.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter the Horse Food Name.","Error.",JOptionPane.ERROR_MESSAGE); 
entryError = true; 

}
if(FoodQuantity.getText().equals("")&& entryError==false)
{
JOptionPane.showMessageDialog(null, "Enter the Horse Food                  
Quantity.","Error.",JOptionPane.ERROR_MESSAGE); 
entryError = true; 

}
if(FoodTime.getText().equals("")&& entryError == false)
{
JOptionPane.showMessageDialog(null, "Enter the Horse FoodTimes.","Error.",JOptionPane.ERROR_MESSAGE); 
entryError = true; 

  }

  if(entryError == false)
   {


try {
    FileWriter tWriter = new FileWriter("HorseNutrition.txt",true);
    PrintWriter tPrinter = new PrintWriter(tWriter);

    tPrinter.println(HorseID + "," + FoodName.getText() + "," + FoodQuantity.getText() + "," + FoodTime.getText());
    tPrinter.close(); 

    JOptionPane.showMessageDialog(null, "Nutritional data has been saved.","Info",JOptionPane.INFORMATION_MESSAGE); 


}
catch(Exception error) {
    System.out.println("Error in saving nutritional data");
}
   }

Any help would be greatly appreciated! 任何帮助将不胜感激!

That HorseID seems to be an object (or to be precise, it looks like JTextField object). HorseID似乎是一个对象(或更确切地说,它看起来像JTextField对象)。 Outputting it as it is will launch it's .toString() method and output some information about that specific JTextField object. 照原样输出将启动它的.toString()方法,并输出有关该特定JTextField对象的一些信息。 If you want to output the text which you have in that field use: 如果要输出该字段中的文本,请使用:

HorseID.getText()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM