Need help with Arrays..and TextFields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rei008
    New Member
    • Nov 2008
    • 7

    Need help with Arrays..and TextFields

    the Arrays wouldn't get words typed in TextField whenever i press the Add button..in my gui..need help..please..
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by rei008
    the Arrays wouldn't get words typed in TextField whenever i press the Add button..in my gui..need help..please..
    Your remark is too vague; about anything can be wrong. Please be a bit more
    specific and show us some relevant code (not all your code please) and tell us
    what is happening and what should have happened.

    kind regards,

    Jos

    Comment

    • rei008
      New Member
      • Nov 2008
      • 7

      #3
      sorry..here's the part with the Add button..

      well..hmm..let' s see..Array data[] wouldn't get the values from my textfield, using txtEntries.getT ext() and..i have a display button..after pressing Add button..when i press the Display button the values from data[0] - data[2] are displayed along with a number of "null"s depending on my Array size..

      so i figured that my Array data[] wouldn't get any value from textfield, using txtEntries.getT ext()..ahahaha. .though i don't know how to solve this prob.


      Code:
      public void actionPerformed(ActionEvent evt) 	{
             int nLimit = Integer.parseInt(txtLimit.getText()); 			
             String[] data = new String[nLimit]; 	
             int ctr2 = Integer.parseInt(txtAvail.getText()); 
             DictionaryTest call = new DictionaryTest(); 	
             data[0] = "hey"; 		
             data[1] = "weee"; 		
             data[2] = "huu"; 	
             int ctr3 = 3;  				 
         if (evt.getActionCommand().equals("ADD")) {
               if(ctr2 > 0) { 			
               data[ctr3] = txtEntry.getText();		
               ctr3++; 			
               ctr2--; 			
               txtAvail.setText(String.valueOf(ctr2)); 			  
               txtEntry.setText(""); 			 			
         } 			 		
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Remember that System.out.prin tln() is a fine poor man's debugger. Stick a couple
        of these lines in your code and print out the value(s) of your variables and any other
        value you're interested in. That actionPerformed () method is quite a piece of
        convoluted logic that depends on quite a bit of text components and other
        components. Can you explain in clear simple English what it is supposed to do?
        Also note that your 'data' array is a local variable which means that it is gone
        when your actionPerformed () method terminates.

        kind regards,

        Jos

        Comment

        • rei008
          New Member
          • Nov 2008
          • 7

          #5
          Originally posted by JosAH
          Remember that System.out.prin tln() is a fine poor man's debugger. Stick a couple
          of these lines in your code and print out the value(s) of your variables and any other
          value you're interested in. That actionPerformed () method is quite a piece of
          convoluted logic that depends on quite a bit of text components and other
          components. Can you explain in clear simple English what it is supposed to do?
          Also note that your 'data' array is a local variable which means that it is gone
          when your actionPerformed () method terminates.

          kind regards,

          Jos
          i see..so, that's why..haha..than ks..

          hmm..well..i wanted to add new values into my array by typing a word in the textfield and then add it to the array with the add button..can't seem to find a solution to this anywhere..

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by rei008
            i see..so, that's why..haha..than ks..

            hmm..well..i wanted to add new values into my array by typing a word in the textfield and then add it to the array with the add button..can't seem to find a solution to this anywhere..
            Neither do I because I still don't understand what your problem is; your description
            of the problem is still utterly vague. One small tip: use an ArrayList<Strin g> instead.

            kind regards,

            Jos

            Comment

            • rei008
              New Member
              • Nov 2008
              • 7

              #7
              Originally posted by JosAH
              Neither do I because I still don't understand what your problem is; your description
              of the problem is still utterly vague. One small tip: use an ArrayList<Strin g> instead.

              kind regards,

              Jos
              sorry, i'm not good at explaining stuff..

              here's that part in my gui..
              hmm..is there another way to add an entry into the array using a textfield and a button? i don't know how to solve the 'data' Array local variable problem..haha.. where should i declare my 'data' Array and variables?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by rei008
                sorry, i'm not good at explaining stuff..

                here's that part in my gui..
                hmm..is there another way to add an entry into the array using a textfield and a button? i don't know how to solve the 'data' Array local variable problem..haha.. where should i declare my 'data' Array and variables?
                Make 'data' a member variable, not a local variable. Also make data a simple
                List<Integer> object (see the API documentation for the List interface).

                kind regards,

                Jos

                Comment

                • rei008
                  New Member
                  • Nov 2008
                  • 7

                  #9
                  Originally posted by JosAH
                  Make 'data' a member variable, not a local variable. Also make data a simple
                  List<Integer> object (see the API documentation for the List interface).

                  kind regards,

                  Jos

                  thank you very much..i'll try changing my codes..

                  Comment

                  Working...