Workshop 2007 JRE Java Support
Installation
Home
CalcTool
Java Applets
Friends/Family
Gardening
Woodworking
Forums
private void  computeButtonMouseClicked(java.awt.event.MouseEvent evt) {                                           
	int answer=0;
int n=0;
String inputString=null;       
inputString=nField.getText(); n=Convert.toInt(inputString);         for (int i=1; i<=n; i++){ answer+=i; } loopLabel.setText(""+answer); }

I should point out that we can eliminate the use of the inputString variable by combining the two steps of getting the text fromnField and Converting it to an int. If we do this, the two lines

inputString=nField.getText();
n=Convert.toInt(inputString);

become the single statement

n=Convert.toInt(nField.getText());

 

 

 

Sample Code 2