1

I have this button and a text field and i wanted to add value on the variable when the button is clicked everything is working apart from I'm unable to add value to string variable

For example if i put value 20 on tempvalue string it should have 20 and i put 30 it should have 50 but what i get is null2050.
I tried += operator which didn't work.

Isn't there any operator that keep adding value on top it or do i have to write new method ?

private String tempvalue;

btnEnter.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String getTxt = textField.getText();
        tempvalue += getTxt;

        System.out.println(tempvalue);

    }
});
2
  • 3
    You show way too little code for us to see what is going wrong. What is "tempValue" ? What is "gtTxt" ? Are they declared, are they initialized ? Do they have a value they refer to ? Commented Mar 26, 2015 at 13:13
  • 5
    You are concatenating strings instead of adding up numbers. Commented Mar 26, 2015 at 13:14

3 Answers 3

4

You get Strings from Textfields.

String input = getTxt;

You have to parse the String to an integer or any other number type.

int value = Integer.parseInt(input);

Then you can do calculations.

You should also always check if the User Input really is a number. Use try/catch to avoid wrong input:

int value = 0;
int firstValue = 5; //example variable
try{
    value = Integer.parseInt(input);
}catch(Exception e1){
    System.out.println("Your input could not be parsed to a number");
}
int result = firstValue + value; //always be sure all your values are numbers and not strings
System.out.println("Result: "+result);

In total:

private int tempvalue = 0;

btnEnter.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String getTxt = textField.getText();
        int value = 0;

        try{
            value = Integer.parseInt(getTxt);
        }catch(Exception e1){
            System.out.println("Your input could not be parsed to a number");
        }

        tempvalue += value;

        System.out.println("Result: "+tempvalue);

        }
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks i will try your method. i'm used to visual basic and now i have to learn java which is just different for me so im struggling with java atm T_T.
I don't see where firstValue is being declared
@CandiedOrange firstValue was just an example to show the try/catch. The applied example for the user is the total example. But I declared it now.
thanks it works just change "catch(Exception e)" to "catch(Exception e1)" and it worked
Oh... I oversaw the ActionEvent, sorry
1

You're simply concatenating your Strings. In fact you start from null then you add 20 but as a String and then 30 but always as a String. Transform in each step into a number and then you have your result done.

Comments

1

As commented by @Jesper the code are concatenating strings and not applying computations such as sum, subtraction and son on ...

So, try out to change the code to convert from java.lang.String to java.langInteger


Integer tempValue += new Integer( getText );
System.out.println( tempvalue ); 

or using a static method from Integer wrap class

Integer tempValue += Integer.parseInt( getText );
System.out.println( tempvalue );

or then using a int Java type (with auto-box automatically)

int tempValue += Integer.parseInt( getText ).intValue();
System.out.println( tempvalue );

Be careful about string to integer conversions. This can rise a NumberFormatException on runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.