Jump to content

How to use an array to store numbers which has been entered to text field in java?

i want to make a calculator in java.i want to take user inputs from jbuttons and make it display in the textfield and i want to store each digit on a array

Link to comment
Share on other sites

Link to post
Share on other sites

Create Empty/Prefilled array

On Click, put number in array and then call a method which updates the textfield, putting out the numbers in the array.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Organized said:

Create Empty/Prefilled array

On Click, put number in array and then call a method which updates the textfield, putting out the numbers in the array.

     
   i have currently done it this way. it doesnt work. i want the array to  make the number appear on the correct position of the textfield. 
 
 

   int counter = 0;
       int[] myArray=null;
 if (e.getSource() == button)
        {
         
  
       textArea.insert("1",myArray[counter]);
  counter++;
   
        }
     
 if (e.getSource() == button_1)
        {
             
            textArea.insert("2",myArray[counter]);
             counter++;
        }
  
 }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well, looks like you're not understanding my idea. I pseudo-coded it for you:

 

ArrayList<int> numbers = new ArrayList<int>();

function onClick1() { // repeat this for every button, as you already did
	numbers.add(1);
    updateText();
}

// [...]

function updateText() {
	for (int i = numbers.size(); i < 1; i--) {
    	textArea.insert(numbers.get(i));
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×