Problems with Integer.parseInt ()

2

I am making an application of the game of roulette, where to bet the player must press buttons.

I can not get the number of bets placed on a JLabel calculated.

What the JLabel must return is contador + (JLabelname.getText())-->INT

Here I leave the class:

public class Chip extends JButton implements MouseListener {
    public int contador=0;

    int suma=0;
    public String ap;
    public JLabel apuesta=new JLabel();

    public Chip(JLabel a) {
        setEnabled(true);
        setVisible(true);
        apuesta=a;
        setContentAreaFilled(true);
        setBorderPainted(true);
        setBorder(new LineBorder(Color.BLACK, 2));
        setText(null);

        this.addMouseListener(this);





    }
    public void setMostrarFichas()// Si ficha >0 Se muestra
    {
        setText(""+contador);
        if(contador<=0){
            setOpaque(false);
            setContentAreaFilled(false);
            setBorderPainted(false);
        }else{
            setOpaque(true);
            setContentAreaFilled(true);
            setBorderPainted(true);
        }

    }


    @Override
    public void mouseClicked(MouseEvent e) {

            //suma=getIntLabel(suma);  **AQUI ESTARIA EL PROBLEMA**

            if((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK){
                contador++;     

            }else{
                if(contador>0){
                    contador--;}

            }
            suma=suma+contador;
            setMostrarFichas();

            apuesta.setText(""+suma);

    }

    public int getIntLabel(int x)
    {
        if(apuesta.getText() != null){

            x= Integer.parseInt(apuesta.getText());
        }
        return x;

    }

The error:

at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Chip.getIntLabel(Chip.java:80)
at Chip.mouseClicked(Chip.java:59)
    
asked by Frachy 14.12.2017 в 05:58
source

1 answer

1

You have a small conceptual error here; If you write a conditional like the one you have:

if( apuesta.getText() != null ) {
  x = Integer.parseInt( apuesta.getText() );
}

something redundant would be reflected, because the content of apuesta will always be different from null , a "" is different from null, then it will always try to parsing to int , naturally when the JLabel blank will take the 'empty' and when wanting to do the conversion it will throw the beautiful NumberFormatException ; That actually happens the first time the program executes the suma = getIntLabel(suma); instruction.

You could modify that condition, for example, in the form:

if( !apuesta.getText().isEmpty() ) {
  x = Integer.parseInt( apuesta.getText() );
}

Now with the conditional in this snippet (and especially assuming that apuesta always contains what the variable suma that is of type int and nothing else) it is obtained that when the label is not empty assign it to x the respective conversion to whole.

Edit: The above is based on the structure of the source code you provided, there are logically more ways to develop a totally different solution that may not even have a conditional to avoid certain redundancies ( for example, always verifying that apuesta is not empty knowing that it is not ), anyway it is always good to understand the small and basic details.

    
answered by 14.12.2017 в 08:30