0

I am working on a program which uses jLabels and I need to check if label is empty or not. If it's empty it should just pop up a note that it's empty and nothing else, but it actually throws a lot of errors. I'm using label.getText().isEmpty().
Here's the code:

if(Integer.parseInt(najboljsi1.getText())<1||Integer.parseInt(najboljsi1.getText())>17||najboljsi1.getText().isEmpty()||
       Integer.parseInt(najboljsi2.getText())<1||Integer.parseInt(najboljsi2.getText())>17||najboljsi2.getText().isEmpty()||
       Integer.parseInt(najboljsi3.getText())<1||Integer.parseInt(najboljsi3.getText())>17||najboljsi3.getText().isEmpty()||
       Integer.parseInt(najslabsi1.getText())<1||Integer.parseInt(najslabsi1.getText())>17||najslabsi1.getText().isEmpty()||
       Integer.parseInt(najslabsi2.getText())<1||Integer.parseInt(najboljsi2.getText())>17||najslabsi2.getText().isEmpty()||
       Integer.parseInt(najslabsi3.getText())<1||Integer.parseInt(najslabsi3.getText())>17||najslabsi3.getText().isEmpty())
    {
        jLabel101.setForeground(Color.red);
        jLabel101.setText("Eno ali več vnesenih števil ni v pravilnem obsegu (1-16)!");
    }
    else
    {
        jLabel101.setText("");
        int a=Integer.parseInt(najboljsi1.getText());
        tabela[a-1]+=3;
        int b=Integer.parseInt(najboljsi2.getText());
        tabela[b-1]+=2;
        int c=Integer.parseInt(najboljsi3.getText());
        tabela[c-1]+=1;
        int d=Integer.parseInt(najslabsi1.getText());
        tabela[d-1]-=3;
        int e=Integer.parseInt(najslabsi2.getText());
        tabela[e-1]-=2;
        int f=Integer.parseInt(najslabsi3.getText());
        tabela[f-1]-=1;
        najboljsi1.setText("");
        najboljsi2.setText("");
        najboljsi3.setText("");
        najslabsi1.setText("");
        najslabsi2.setText("");
        najslabsi3.setText("");
        count++;
        jLabel1.setText("Učenec "+count);
    }

Everything else in if statement works ok, if value is lower than 1 or higher than 16, it throws a pop up.

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
ma20tej
  • 3
  • 1
  • 1
    Could you add what error are you getting ? Also, najboljsi1.getText().isEmpty() can be the first condition in the 'if' statement – Abbin Varghese Feb 26 '19 at 10:34

3 Answers3

2

If you do Integer.parseInt(najboljsi2.getText()) on a label with the textn "" (empty String), it won't be an integer. An exception will be thrown.

2

Yes, you must test najboljsi1.getText().isEmpty() BEFORE any parsing of najboljsi1.getText(). Your if would become:

if(najboljsi1.getText().isEmpty()||Integer.parseInt(najboljsi1.getText())<1||Integer.parseInt(najboljsi1.getText())>17||
       najboljsi2.getText().isEmpty()||Integer.parseInt(najboljsi2.getText())<1||Integer.parseInt(najboljsi2.getText())>17||
etc...
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
1

I think your problem is in the use of the "Integer.parseInt" without any check! If, for example, the variable contains an empty string, it will throw an Exception and your if clause will never work!

I would manage the situation with a double check.

  1. Check if it is already a number (this guide could help check-if-variable-is-a-number-in-javascript)
  2. Then, if it is a string, check if it is empty and if it actually contains a string (the following post could also help check-whether-an-input-string-contains-a-number-in-javascript)

Ps. Sorry, I modified the answer with some extra links

t.montanaro
  • 123
  • 11