Results 1 to 7 of 7
  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default Adding To An Integer

    Feel free to laugh at me for this one, but I was building a rather complicated mod for a game and when I was trying to make so that when you hit a button it started a repeat loop. Unfortunately I couldn't figure out how to change the value of an int that was already set. I tried counter++ (because I used counter as a variable,) I tried to do counter.setValue, and many others. I have tried to look this up but I was unable to figure out how.

    If what I was saying up there didn't make any since then basically I just want to add or subtract 1 from a variable.

    Variable:
    int counter = 1;

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Using counter++ will work. Why didn't work for you? We have no idea unless you post the relevant code.
    Java Code:
    int counter = 1;
    System.out.println(counter);
    counter++;
    System.out.println(counter);
    Output is:
    1
    2

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Unless you fell for the ol post increment trick
    Java Code:
    int counter = 1;
    System.out.println(counter);
    counter = counter++;
    System.out.println(counter);
    Output is:
    1
    1

    If this what you have done then do a google search as this has been asked/discussed/explained ad infinitum.

  4. #4
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Using counter++ will work. Why didn't work for you? We have no idea unless you post the relevant code.
    Java Code:
    int counter = 1;
    System.out.println(counter);
    counter++;
    System.out.println(counter);
    Output is:
    1
    2
    your right I wasn't thinking I had it like this.
    Java Code:
     int counter = 1;
            
            gwa.bu.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
               
                	counter++
                }
                
            });
            while(counter < 1){
    unfortunately I don't know that I can change the other counter from different brackets can I?

  5. #5
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    I actually think I have an idea I will make a separate class with that variable being public and just call the variable from the other class

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    You probably got some error saying that the variable needed to be declared as final as you are trying to access it from the anonymous inner class. But you cannot make it final as you want to change its value. What you need to do is make the variable ans instance variable of the outer class and not local to the method.

  7. #7
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You probably got some error saying that the variable needed to be declared as final as you are trying to access it from the anonymous inner class. But you cannot make it final as you want to change its value. What you need to do is make the variable ans instance variable of the outer class and not local to the method.
    alright thanks

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  2. Adding integer to arraylist
    By powerpravin in forum New To Java
    Replies: 2
    Last Post: 04-03-2011, 07:21 AM
  3. Adding an integer to a double?
    By tokoolio in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 06:57 PM
  4. adding integer to the JPanel
    By navid in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 04:26 PM
  5. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •