Help with simple applet
Hi,
I need help with a simple applet application. It must allow the user to enter a list of integers, while displaying the largest integer input so far and the smallest integer input so far. This is what I'v got so far;
import java.applet.* ;
import java.awt.* ;
import java.awt.event.* ;
public class GUIAppletExercise2 extends Applet implements ActionListener
{
// Declare the GUI components globally
Label inputLabel, smallestLabel, largestLabel ;
TextField inputBox, smallestBox, largestBox ;
// Declare integer variables to hold the number input, the smallest
// number so far, largest number so far and a temp variable to
// be used when comparing and swapping the smallest and largest integers
int numberInteger, smallestInteger, largestInteger, temp ;
// Declare variables to hold string versions of the three integer
// variables for placing in the TextFields
String inputString, smallestString, largestString ;
public void init ()
{
// Create the labels
inputLabel = new Label ( "Type in an integer and press ENTER" ) ;
smallestLabel = new Label ( "Smallest number so far" ) ;
largestLabel = new Label ( "Largest number so far" ) ;
// Create the TextFields
inputBox = new TextField ( 12 ) ;
smallestBox = new TextField ( 3 ) ;
largestBox = new TextField ( 12 );
// Add the components to the applet window
add ( inputLabel ) ;
add ( inputBox ) ;
inputBox.addActionListener ( this ) ;
add ( smallestLabel ) ;
add ( smallestBox ) ;
smallestBox.setEditable ( false ) ;
add ( largestLabel ) ;
add ( largestBox ) ;
largestBox.setEditable ( false ) ;
} // End of init ()
public void actionPerformed ( ActionEvent e )
{
try
{
// Extract a string from inputBox
inputString = inputBox.getText () ;
// Convert this string to an integer
numberInteger = Integer.parseInt ( inputString ) ;
// Clear the status box
showStatus ( "" ) ;
// Compare the number input by the user and update the smallesBox
// or largestBox if neccessary
if ( numberInteger > largestInteger )
{
largestInteger = numberInteger ;
largestInteger = temp ;
temp = numberInteger ;
}
else if ( numberInteger < smallestInteger )
{
smallestInteger = numberInteger ;
smallestInteger = temp ;
temp = numberInteger ;
}
// Convert smallestinteger and largestInteger to strings
// and place in smallestBox and largestBox
smallestString = Integer.toString ( smallestInteger ) ;
smallestBox.setText ( smallestString ) ;
largestString = Integer.toString ( largestInteger ) ;
largestBox.setText ( largestString ) ;
// Clear the text in InputBox
inputBox.setText ( "" ) ;
}
catch ( NumberFormatException entry )
{
// Display an error message
showStatus ( "Error: Invalid Input" ) ;
// Clear the string in inputBox
inputBox.setText ( "" ) ;
}
} // End of actionPerformed method
} // End of class
If anybody can help, it will be most appreciated.
Thanks in advance
[3316 byte] By [
Rory_Ta] at [2007-11-26 20:16:41]

I can't see any problem as such. Are you facing any problems?
Hi,
Yes when I run it, it seems to update the largestInteger box ok, but is always a step behind. eg. first time I run it, say I enter 55, the smallestInteger and largestInteger will update to 0 each. then if i enter 100 and press enter, it will update the largestBox with the previous integer eg.55 instead of the integer I have just entered (100).
Also the smallestBox box will not update - eg if I entered four integers - say 100, 200, 300, 45 - it will always stay at 0 - despite 0 not being entered by the user - the smallest number entered by the user in this case would be 45.
I haven't faced this type of problem till date . Try calling validate( ) on the text field after you set the text.
smallestString = Integer.toString ( smallestInteger ) ;
smallestBox.setText ( smallestString ) ;
smallestBox.validate();
largestString = Integer.toString ( largestInteger ) ;
largestBox.setText ( largestString ) ;
largestBox.validate();
// Clear the text in InputBox
inputBox.setText ( "" ) ;
inputBox.validate();
Message was edited by:
qUesT_foR_knOwLeDge
HiThanks for the fast reply.But it still has the same problem when I insert the new code - also have just started first year in programming course and haven't used validate yet, so don't think I could use it even if it worked lolThanks anyway
You can rathe have one button called submit that you will have to press everytime you add an input than add a listener to every box .
The trouble is with the logic. The logics wrong.
This works
I have used a button. After you enter a no in the first text field press the button and the othe two text fields will get updated accordingly.
import java.applet.* ;
import java.awt.* ;
import java.awt.event.* ;
public class GUIAppletExercise2 extends Applet implements ActionListener
{
// Declare the GUI components globally
Label inputLabel, smallestLabel, largestLabel ;
TextField inputBox, smallestBox, largestBox ;
int flag=0;
Button button=new Button("Enter");
// Declare integer variables to hold the number input, the smallest
// number so far, largest number so far and a temp variable to
// be used when comparing and swapping the smallest and largest integers
int numberInteger=0, smallestInteger=0, largestInteger=0, temp =0;
// Declare variables to hold string versions of the three integer
// variables for placing in the TextFields
String inputString, smallestString, largestString ;
public void init ()
{
// Create the labels
inputLabel = new Label ( "Type in an integer and press ENTER" ) ;
smallestLabel = new Label ( "Smallest number so far" ) ;
largestLabel = new Label ( "Largest number so far" ) ;
// Create the TextFields
inputBox = new TextField ( 12 ) ;
smallestBox = new TextField ( 3 ) ;
largestBox = new TextField ( 12 );
// Add the components to the applet window
add ( inputLabel ) ;
add ( inputBox ) ;
add ( smallestLabel ) ;
add ( smallestBox ) ;
add ( largestLabel ) ;
add ( largestBox ) ;
add(button);
button.addActionListener(this);
smallestBox.setText ( "0" ) ;
largestBox.setText ( "0" ) ;
} // End of init ()
public void actionPerformed ( ActionEvent e )
{
try
{
// Extract a string from inputBox
inputString = inputBox.getText () ;
// Convert this string to an integer
numberInteger = Integer.parseInt ( inputString ) ;
// Clear the status box
showStatus ( "" ) ;
// Compare the number input by the user and update the smallesBox
// or largestBox if neccessary
if ( numberInteger > largestInteger )
{
largestInteger = numberInteger ;
largestString = Integer.toString ( largestInteger ) ;
largestBox.setText ( largestString ) ;
if(flag==0)
{
smallestBox.setText ( largestString ) ;
smallestInteger=largestInteger;
flag=1;
}
}
if ( numberInteger < smallestInteger )
{
smallestInteger = numberInteger ;
smallestString = Integer.toString ( smallestInteger ) ;
smallestBox.setText ( smallestString ) ;
}
// Convert smallestinteger and largestInteger to strings
// and place in smallestBox and largestBox
// Clear the text in InputBox
inputBox.setText ( "" ) ;
}
catch ( NumberFormatException entry )
{
// Display an error message
showStatus ( "Error: Invalid Input" ) ;
// Clear the string in inputBox
inputBox.setText ( "" ) ;
}
} // End of actionPerformed method
} // End of class
Message was edited by:
qUesT_foR_knOwLeDge
null
I have modified the code and it works fine
Hopefully this is what you need
The problem was that by default the minimum value was 0 because of which any number you were entering was never displayed on screen
Any ways check if it is clear or you still seems to have some problem
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class GUIAppletExercise2 extends Applet implements ActionListener {
// Declare the GUI components globally
Label inputLabel, smallestLabel, largestLabel;
TextField inputBox, smallestBox, largestBox;
// Declare integer variables to hold the number input, the smallest
// number so far, largest number so far and a temp variable to
// be used when comparing and swapping the smallest and largest integers
int numberInteger, smallestInteger, largestInteger, temp;
// Declare variables to hold string versions of the three integer
// variables for placing in the TextFields
String inputString, smallestString, largestString;
boolean flagFirstTime = true;
public void init() {
// Create the labels
inputLabel = new Label("Type in an integer and press ENTER");
smallestLabel = new Label("Smallest number so far");
largestLabel = new Label("Largest number so far");
// Create the TextFields
inputBox = new TextField(12);
smallestBox = new TextField(3);
largestBox = new TextField(12);
// Add the components to the applet window
add(inputLabel);
add(inputBox);
inputBox.addActionListener(this);
add(smallestLabel);
add(smallestBox);
smallestBox.setEditable(false);
add(largestLabel);
add(largestBox);
largestBox.setEditable(false);
} // End of init ()
public void actionPerformed(ActionEvent e) {
try {
// Extract a string from inputBox
inputString = inputBox.getText();
// Convert this string to an integer
numberInteger = Integer.parseInt(inputString);
// Clear the status box
showStatus("");
// Compare the number input by the user and update the smallesBox
// or largestBox if neccessary
if (flagFirstTime) {
largestInteger = numberInteger;
smallestInteger = numberInteger;
flagFirstTime = false;
} else {
if (numberInteger > largestInteger) {
largestInteger = numberInteger;
// largestInteger = temp;
// temp = numberInteger;
} else if (numberInteger < smallestInteger) {
smallestInteger = numberInteger;
// smallestInteger = temp;
// temp = numberInteger;
}
}
// Convert smallestinteger and largestInteger to strings
// and place in smallestBox and largestBox
smallestString = Integer.toString(smallestInteger);
smallestBox.setText(smallestString);
largestString = Integer.toString(largestInteger);
largestBox.setText(largestString);
// Clear the text in InputBox
inputBox.setText("");
} catch (NumberFormatException entry) {
// Display an error message
showStatus("Error: Invalid Input");
// Clear the string in inputBox
inputBox.setText("");
}
} // End of actionPerformed method
} // End of class