Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accepting minimum no. of chars, digit figure & checkbox

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello again everyone,

I was wondering how I would go about coding to see if

1. A textfield has a minimum of say 15 characters (ie, Description)

2. Another field that checks for a six digit figure (ie. Price)

3. Another checkbox field to see if an item is taxable or not.


This question is an extension and related to the one I posted titled:

"Different variable type for Vector or array? Similar to Structure in C"

thread269-144403

I believe the logic involves creating a 2 JTextFields and JCheckbox... then when the event is clicked or entered, it does the ff:

1. Checks to see if Description field contains a least 15 characters

2. Checks to see if the Price field contains at least 6 digits

3. Checks to see if the Taxable field is checked or not.

Once this is done, it does the calculations and loads it into the ArrayList.

All help is appreciated.

Cheers,

Christopher






 
Is this possible? or should I look into the Keyboard events?

Any ideas?

Thanks,

Christopher
 
You definitely going to need to use events. You will most likely want to take advantage of Change Events (take a look at ChangeListener). When the event is fired you are going to want to check the length of the Strings. Are you familiar with Java's Event Model?

Anyways, to check the length of a String use String.length(). Example:
Code:
String test = "test";
System.out.println("String test is of length :" + test.length());
 
Hi again Wushutwist,

Thank you for your suggestion. I have just started looking listeners and event handling.

I used KeyListener to check for the number of characters (I've accounted for the backspace as well (i.e. -1) for the description field.

How can I make sure then that the Price field is at least 6 digits and of double type?

And that the checkbox is selected and returns a true|false so I could use them as arguments on another method that adds this record?

For example:

after checking the fields....

addItem(descriptionField, priceField, source);

....

public void addItem(String name, double thePrice, boolean taxState)
{
ArrayList myStuff = new ArrayList();

Item cidx = new Item(name, thePrice, taxState);
myStuff.add(cidx);
numrecords++;

showList(myStuff);

}

etc....

Wait a minute... I just had a thought X-) ... If I created a reference to an ArrayList called "myStuff" everytime this method is called (ie. item added), I will only have 1 record no matter how many items I've added, correct?

Am I correct to conclude that it is because I create the same ArrayList "myStuff" that it will overwrite whatever I had in the "myStuff" ArrayList? so if I were to print them out, all I would see is the last entry, correct?

Would it be better if I created "myStuff" ArrayList, during the start of the program?

Any ideas?

Christopher






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top