Results 1 to 4 of 4
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Store integer or double value? To use current value and initial value?

    I was just curious how I'd go about actually storing a previous int or double value. Currently I am trying to use a textfield to take an inputted number and count to that number. What I am trying to do is let a user start at 1 and count linearly. But when the user puts a new number into a textbox which is greater than it's last value+1 it will loop that many times until it hits that number. I looked this up but I didn't actually find any real way of storing a value. If I can't store a value what should I do in this case?

    Java Code:
    double numberinput = Double.parseDouble(jTextField2.getText());
    double numberinputorgvalue = numberinput-1;
    
    // if the inputted number is greater than itself -1
     if(numberinput>numberinputorgvalue+1){   //<-- this is the part I'm stuck on.  
    //I'm trying to use the old value
        
           for(numberinput=numberinput;numberinputorgvalue<numberinput;numberinputorgvalue+=1){
     //loop that many times until you hit that number
            double currentItem = Double.parseDouble(jTextField2.getText());
            currentItem--;
            jTextField2.setText(String.valueOf(currentItem).indexOf(".") < 0 ? String.valueOf(currentItem) : String.valueOf(currentItem).replaceAll("0*$", "").replaceAll("\\.$", ""));
            jProgressBar1.setValue(--progress);
            jProgressBar1.setString(progress + "/40");
    
        }
    
    }
    Last edited by innovo; 08-24-2015 at 04:38 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Store integer or double value? To use current value and initial value?

    When you say "store a value" do you mean to store it in a file or simply retain it somewhere for another prompt by the user?

    And whether you use integer or floating point is up to you. But it is unlikely that a "hit" will occur when incrementing integers towards a floating point value (unless you check for greater than).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: Store integer or double value? To use current value and initial value?

    Quote Originally Posted by jim829 View Post
    When you say "store a value" do you mean to store it in a file or simply retain it somewhere for another prompt by the user?

    And whether you use integer or floating point is up to you. But it is unlikely that a "hit" will occur when incrementing integers towards a floating point value (unless you check for greater than).

    Regards,
    Jim
    I didn't mean to store it in a file, What I mean is let's say someone has "1" in their jTextField. Then all of a sudden they type in "8". I want to loop until I hit the number "8". For another instance let's say the value is "32", and the user inputs "57". I want to use a for-loop to print to a file 57-32 times until it hits 57.

  4. #4
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: Store integer or double value? To use current value and initial value?

    You add a listener to the JTextField which will fire when the user changes, updates or inserts into that JTextField. You can actually put the loop right in these method calls or you can call another method of your class to handle it there. Also there is a case where a listener can fire your function twice so I did a search for a stackoverflow question that is similar to yours
    java - Value Change Listener to JTextField - Stack Overflow

Similar Threads

  1. Double keeps rounding to integer?
    By LucienMontierre in forum New To Java
    Replies: 2
    Last Post: 06-29-2012, 05:12 AM
  2. convert integer to double
    By himanwish in forum New To Java
    Replies: 4
    Last Post: 03-17-2011, 01:55 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. How do you store a random integer in an array?
    By Unknown in forum New To Java
    Replies: 6
    Last Post: 03-21-2010, 10:40 PM
  5. How to extract integer value out of a double value?
    By javaexplorer in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2010, 05:32 AM

Posting Permissions

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