Samooramad
asked on
textfield for integer or string
Hi experts,
I need code for the following:
I have a textfield and two buttons. One button should be expect an integer from the text field (between 1-99) and when pressed it changes an integer variable.
the other button should expect a String and then change the "name" variable
thanks
also if anyone can help me with this question I accidently forgot to place it in the java topic area:
https://www.experts-exchange.com/questions/21154053/changing-an-option-pane.html
thanks everyone
I need code for the following:
I have a textfield and two buttons. One button should be expect an integer from the text field (between 1-99) and when pressed it changes an integer variable.
the other button should expect a String and then change the "name" variable
thanks
also if anyone can help me with this question I accidently forgot to place it in the java topic area:
https://www.experts-exchange.com/questions/21154053/changing-an-option-pane.html
thanks everyone
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Allow me to correct:
1)
void jButton4_actionPerformed(A ctionEvent e)
{
try { // <<<<<<<< try/catch needed
int thisVal = Integer.parseInt(jTextFiel d1.getText ());
if( thisVal >= 1 && thisVal <= 99) // <<<<<< && instead of ||
myValu = thisVal;
} catch (NumberFormatException ex) {
}
}
Remark: I don't see the added value compared with my previous comment
2)
If you want to check if the string only contains A-Z, a-z, spaces and dots, you can better do this:
void jButton5_actionPerformed(A ctionEvent e) {
if ( jTextField1.getText().matc hes("([A-Z a-z \\.])+?") )
name = jTextField1.getText();
}
That's the power of regular expressions!
1)
void jButton4_actionPerformed(A
{
try { // <<<<<<<< try/catch needed
int thisVal = Integer.parseInt(jTextFiel
if( thisVal >= 1 && thisVal <= 99) // <<<<<< && instead of ||
myValu = thisVal;
} catch (NumberFormatException ex) {
}
}
Remark: I don't see the added value compared with my previous comment
2)
If you want to check if the string only contains A-Z, a-z, spaces and dots, you can better do this:
void jButton5_actionPerformed(A
if ( jTextField1.getText().matc
name = jTextField1.getText();
}
That's the power of regular expressions!
Thanks zzynx for correcting my mistakes.
ASKER
thanks both of you. I'm working on it.. will get back to you
ASKER
excelent help as always zzynx :)
thanks Naeemg for your help also.. I forgot I need to check for letters and spaces :)
thanks Naeemg for your help also.. I forgot I need to check for letters and spaces :)
ASKER
hey if you guys have a little time please help with the question I mentioned above.. Its driving me nutts :)
thanks
button1.addActionListener(
public void actionPerformed(ActionEven
try {
integerVariable = (Integer.valueOf(textField
} catch (NuberFormatException ex) { }
}
});