如何更新文本文件中的Integer值?

时间:2019-03-25 06:55:33

标签: java text nullpointerexception

我有一个名为BookDetails.txt的文本文件

文本文件的内容是标题,作者,出版者,分支机构电话号码和副本数

  

通用书籍:John Doe:印刷公司:FIC Doe:3

     

动物农场:George Orwell:Harcourt:FIC Orw:7

在我的程序中,我已经能够分割每一行并将整数值(用于副本数)存储在字符串数组中。问题是我的程序在检出给图书馆的顾客时无法从指定书的份数中减去一个值,而是返回空指针异常。

public class CheckOutDialog extends javax.swing.JDialog {
private static final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
ArrayList<CheckOut> checkOuts = new ArrayList<CheckOut>();
String bookTitle; 
/** 
 * Creates new form CheckOutDialog
 */
public CheckOutDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    Date currentDate = new Date();
    jTextFieldDate.setText(df.format(currentDate));
}

private void jButtonCheckOutActionPerformed(java.awt.event.ActionEvent evt) 
{                                                
    String firstName = jTextFieldFirstName.getText();
    String lastName = jTextFieldLastName.getText();
    bookTitle = jTextFieldBookTitle.getText();
    String checkOutDate = jTextFieldDate.getText();

    CheckOut checkOutInfo = new CheckOut(firstName, lastName, bookTitle, checkOutDate);

    checkOuts.add(checkOutInfo);

    CheckOutCopy();
}                                               

public void CheckOutCopy()
{
     try {
        File f = new File("BookDetails.txt");
        Scanner fileRead = new Scanner(f);

        boolean foundTitle = false;

        while(fileRead.hasNextLine())
        {
            String textLine = fileRead.nextLine();
            String[] bookInfo = textLine.split(" : ");
            String tempBookTitle = bookInfo[0];


            if(tempBookTitle.trim().equals(bookTitle))
            {
                foundTitle = true;
                break;
            }
        }
        if(foundTitle)
        {             
           CheckCopies();
        }
        else
        {
            JOptionPanes.messageBox("Book was not found in Library Catalog", "Check Out Error");
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }
}

public void CheckCopies()
{
    try {

    BufferedReader reader = new BufferedReader(new FileReader("BookDetails.txt"));
    reader.readLine();
    String firstLine = null;
      while((firstLine = reader.readLine()) != null)
        {
            String textLine = reader.readLine();
            String[] bookInfo = textLine.split(" : ");
            int numberOfCopies = Integer.parseInt(bookInfo[4]);

            if(numberOfCopies == 0)
            {
                if(JOptionPane.showConfirmDialog(null, "Would you like to add the patron to the queue?", "No copies available", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                {
                    AddPatronQueue();
                }
                else
                {
                    JOptionPane.getRootFrame().dispose();
                }
            }
            else 
            {
                numberOfCopies--;
                WriteToFile();
            }
        }
       } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

private void WriteToFile()
{
    WriteFile wf = new WriteFile("CheckOutDetails.txt");

    for(int i = 0; i < checkOuts.size(); i++)
    {
        CheckOut c = checkOuts.get(i);

        String checkOutDetails =  c.getFirstName() + " : " + c.getLastName() + " : " + c.getBookTitle() + " : " + c.getCheckOutDate() + " : " + numberOfCopies;

        wf.write(checkOutDetails);
    }
    wf.close();
}

0 个答案:

没有答案
相关问题