How to put an integer number on a label

Asked

Viewed 774 times

1

I’m trying to put a kind of score counter on a Jlabel ex: you hit a question the score goes to 1 , you miss and nothing happens to the score.

I tried to do it that way :

   private void confirmarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    while (true){
    String pont = " ";
    int pontuacao = Integer.parseInt(pont);
    if (confirmar == a){
        a.setBackground(Color.green);

    }else{
         a.setBackground(Color.red);
    }

   }

}

Error:

Exception in thread "AWT-Eventqueue-0" java.lang.Numberformatexception: For input string: " "

  • 1

    The error is because you are trying to convert empty string to integer.

  • 1

    Thanks again for helping me diego :D , well I ended up solving my problem ( very easy thing ) , I just created an int score = 0; took the while and put Pont.setText(" "+score);

1 answer

1


It’s wrong because you’re declaring pont as String and you’re trying to convert " " for Int and as is obvious will give conversion error. It is impossible convert nothing for all, whole must always have some numerical value.

  • inteiro tem que ter sempre algum valor. that is not exactly true. Not every value can be converted to an integer.

  • You’re right some value numerical, already corrected ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.