Can you help me find out why my Java app isn’t working?

Kasper Gade Andreasen October 12, 2012
Ads by Google

I am designing a simple notebook application with NetBeans. The whole thing works great except for:

private void delNoteButActionPerformed(java.awt.event.ActionEvent evt) {
String nr = nrField.getText();
int nrT = Integer.parseInt(nr);
if (nrT < 0 || nrT < notes.nrOfNotes() || nrT > notes.nrOfNotes()) {
noteDisplay.setText("There is no note stored at note-index\nnr : " + nrT + ".");
} else {
noteDisplay.setText("You have deleted the note that was in\nnote-index nr: " + nrT );

Everything in the rest of the code works, but here – no matter what – I get stuck in the if-sentence.
.notes is a refference to another class. nrOfNotes() method is an ArrayList.size() call. Now the idea is that if the textfield is blank or the number entered does not exist in the ArrayList, it should do the “if” sentence.

However, when I run it, i get stuck in the if sentence. Can anyone help me?

Happy to provide more detail!

  1. Dimal Chandrasiri
    October 12, 2012 at 2:20 pm

    debug the program and see the values at the if statement to get an idea. confirm the values that you expect is the same or else, there might be an error not in the if, but some where else.

  2. Kasper Gade Andreasen
    October 12, 2012 at 10:08 am

    @Bruce - Yes, something like that! I want it to go into the if-statement only if the number doesn't exist in the array... So that, if I have 5 notes, filling from 0-4 in the array and I enter 6, it should give me the error message. But I don't know how to do this.

  3. Bruce Epper
    October 12, 2012 at 3:45 am

    Your if statement is effectively saying if nrT is less than 0 or if it does not equal the number of elements in your array list it is supposed to execute the next block of code. Is that what you really want? You probably want to drop the second condition in your if statement since you only want to print the error message if your note index is less than 0 or greater than the number of notes in your array.

    • Kasper Gade Andreasen
      October 12, 2012 at 10:16 am

      The thing is that, if I remove the if-else condition, everything works fine. I want the program to check whether something is at the index number and if there is, delete it, and if not, give the error message.

      So the idea is that, if I have 3 notes.. Filling index 0-2. I want it to work, so that if I enter a 3 it should give me the error message. But as is, I get stuck in the if sentence.. Even if I have 4 notes entered, it tells me that the note does not exist!

    • Jan Fritsch
      October 12, 2012 at 3:54 pm

      I have no Java skills or experience but I felt the same as Bruce. It feels like either "nrT<0" is out of place or one of the following conditions.

      The "nrOfNotes" (number of notes?) is probably not going to be lower than 0 so having "nrT < 0" and "nrT < nrOfNotes" seems odd to me. At least without knowing the possible results for notes.nrOfNotes().