0

I am trying to delete a line in a file based on 2 parts in the line: the book title and its ID. I want the user to enter both and if they are both in a line, delete the line. This is the code I have however the only problem is that removeLine says "cannot convert from int to string". Does anyone know how to fix this?

          File inFile = new File("books.txt");

          if (!inFile.isFile()) {

            return;
          }


          File tempFile = new File("books.txt" + "1");

          BufferedReader br = new BufferedReader(new FileReader(inFile));
          PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

          String line = null;



          JTextField xField = new JTextField(10);
            JTextField yField = new JTextField(10);

            JPanel myPanel = new JPanel();
            myPanel.add(new JLabel("Title:"));
            myPanel.add(xField);
            myPanel.add(Box.createHorizontalStrut(15)); // a spacer
            myPanel.add(new JLabel("ID:"));
            myPanel.add(yField);

            String removeLine = JOptionPane.showConfirmDialog(null, myPanel,
                "Remove", JOptionPane.OK_CANCEL_OPTION);
            if (removeLine == JOptionPane.OK_OPTION) {



          while ((line = br.readLine()) != null) {
            if (!line.trim().contains(removeLine)) {
              pw.println(line);
              pw.flush();
            }
          }

          pw.close();
          br.close();

          //Delete the original file
          if (!inFile.delete()) {
            System.out.println("Could not delete file");
            return;
          } 

          //Rename the new file to the filename the original file had.
          if (!tempFile.renameTo(inFile)) {
            System.out.println("Could not rename file");
          }
jkjk
  • 101
  • 1
  • 3
  • 12
  • *Just in case anyone was wondering I did try and change removeLine to int but then the contains(removeLine) gives me the error "the method contains (CharSequence) in the type string is not applicable for the argument int" – jkjk Apr 05 '16 at 04:23
  • I think that your solution is explained here http://stackoverflow.com/questions/6555040/multiple-input-in-joptionpane-showinputdialog You don't recover the values introduced by the user from xField and yField. removeLine will contain only JOptionPane.OK_OPTION or JOptionPane.CANCEL_OPTION and not the book to be removed – RubioRic Apr 05 '16 at 04:59

1 Answers1

-1

JOptionPane.showConfirmDialog returns an int not a String, so you should change the type of removeLine to int.

  • Thanks for your reply, I already tried that but then the contains(lineToRemove) does not work because "the method contains (CharSequence) in the type string is not applicable for the argument int" – jkjk Apr 05 '16 at 04:28
  • You need to use a different method in order to get the text they submitted, `JOptionPane.showConfirmDialog` will only give you whether or not they have confirmed. You should do this as follows. You need to store the ID and title they entered by getting `xField.getText()` and `yField.getText()`. Then, check if both of these are in the line separately. – Donald Clintron 7000 Apr 05 '16 at 04:33
  • I am able to use a showInputDialog but it gives me 3 boxes. 2 boxes for the xField and yField and one large box that basically does nothing. – jkjk Apr 05 '16 at 04:35