Help please--cannot get summary button to work?!!!!
Hi all,
I am trying to get a summary of results on a survey, and I did have a portion of it working, but needed to add more code to complete it.
When I hit the display summary button--nothing happens. There is a problem with my logic, but no logic errors come up. I must have created an infinite loop.
Your help is much appreciated!!!!!
Here is the code
[code][
rivate void summaryJButtonActionPerformed( ActionEvent event )
{
//declare variables
double perAgree = 0.00;
double perDisagree = 0.00;
double perNuetral = 0.00;
//calculate percent response for each category
perAgree = ((double)numAgree/total) * 100;
perDisagree = ((double)numDisagree/total ) * 100;
perNuetral = ((double)numNuetral/total) * 100;
//clear old results
summaryJButton.setText("");
//add header to output area
resultsJTextArea.append( "RESPONSE\tFREQUENCY\tPERCENT\tHISTOGRAM");
//display results
resultsJTextArea.append("\n" + "Agree" + "\t" + numAgree + "\t" + perAgree + "%" );
resultsJTextArea.append("\n" + "Disagree" + "\t" + numDisagree + "\t" + perDisagree + "%" );
resultsJTextArea.append("\n" + "No Opinion" + "\t" + numNuetral + "\t" + perNuetral + "%" );
do
{
if (numAgree < total)
resultsJTextArea.append("\t" + "\t" + "\t" + "*");
if (numDisagree < total)
resultsJTextArea.append("\t" + "\t" + "\t" + "*" );
if (numNuetral < total)
resultsJTextArea.append("\t" + "\t" + "\t" + "*");
} while (numAgree + numDisagree + numNuetral <= total);
} // end method summaryJButtonActionPerformed
/code]
[1730 byte] By [
sunydee4a] at [2007-10-2 14:25:18]

Hi and thanks for your help!
I did invoke the method with a handler. Maybe I should have given you the entire code that I have in the first place.
The first part of the code, and the other method seems to be working, so I was focusing on where the problem is.
By the way- tried the system.out.printli, but since nothing happens when I click the summary button, I cannot even get any values to print in the command prompt.
Here is the entire app:
// Application summarized results of a survey
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Survey extends JFrame
{
// JLabel and JTextField read response
private JLabel responseJLabel;
private JTextField responseJTextField;
// JButton initiates display of results
private JButton summaryJButton;
// JTextArea for survey results
private JTextArea resultsJTextArea;
// JButton resets to start again
private JButton resetJButton;
// DECLARE COUNTERS FOR VALID RESPONSES
private int numAgree = 0;
private int numDisagree = 0;
private int numNuetral = 0;
private int total = 0;
// no-argument constructor
public Survey()
{
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up responseJLabel
responseJLabel = new JLabel();
responseJLabel.setBounds( 116, 8, 70, 23 );
responseJLabel.setText( "Response:" );
contentPane.add( responseJLabel );
// set up responseJTextField
responseJTextField = new JTextField();
responseJTextField.setBounds( 215, 8, 70, 23 );
contentPane.add( responseJTextField );
// ADD THE ACTION LISTENER TO THIS FIELD
responseJTextField.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when user clicks responseJButton
public void actionPerformed ( ActionEvent event )
{
responseJTextFieldActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up summaryJButton
summaryJButton = new JButton();
summaryJButton.setBounds( 116, 40, 170, 26 );
summaryJButton.setText( "Display Summary" );
summaryJButton.setEnabled(false);
contentPane.add( summaryJButton );
summaryJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when user clicks summaryJButton
public void actionPerformed ( ActionEvent event )
{
summaryJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up resultsJTextArea
resultsJTextArea = new JTextArea();
resultsJTextArea.setBounds( 16, 78, 400, 100 );
resultsJTextArea.setEditable(false);
contentPane.add( resultsJTextArea );
// set up resetJButton
resetJButton = new JButton();
resetJButton.setBounds( 175, 200, 70, 23 );
resetJButton.setText( "reset" );
contentPane.add( resetJButton );
// ADD THE ACTION LISTENER TO THIS BUTTON
resetJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when user clicks resetJButton
public void actionPerformed ( ActionEvent event )
{
resetJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set properties of application's window
setTitle( "Survey Summary" ); // set title bar text
setSize( 450, 275 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
// method reads user input and accumulates totals
private void responseJTextFieldActionPerformed( ActionEvent event )
{
String input; //used to store user's input
boolean valid;
int response;
//test for no input
if (responseJTextField.getText().equals(""))
{
JOptionPane.showMessageDialog( null,
"Please enter a response to the survey ", "Message",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//read user input and convert to number
input = ( responseJTextField.getText() );
response = Integer.parseInt (input);
// display error when response is not valid
if (response == 1 || response == 2 || response == 3)
{
valid = true;
total++;
summaryJButton.setEnabled (true);
responseJTextField.setText("");
}
else
{
//display error message
JOptionPane.showMessageDialog( null,
"Please enter a valid response ", "Message",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//add totals for each category
if (response == 1)
numAgree++;
if (response == 2)
numDisagree++;
if (response == 3)
numNuetral++;
} // end method responseJTextFieldActionPerformed
// method displays survey summary
private void summaryJButtonActionPerformed( ActionEvent event )
{
//declare variables
double perAgree = 0.00;
double perDisagree = 0.00;
double perNuetral = 0.00;
//calculate percent response for each category
perAgree = ((double)numAgree/total) * 100;
perDisagree = ((double)numDisagree/total ) * 100;
perNuetral = ((double)numNuetral/total) * 100;
//clear old results
summaryJButton.setText("");
//add header to output area
resultsJTextArea.append( "RESPONSE\tFREQUENCY\tPERCENT\tHISTOGRAM");
//display results
resultsJTextArea.append("\n" + "Agree" + "\t" + numAgree + "\t" + perAgree + "%" );
resultsJTextArea.append("\n" + "Disagree" + "\t" + numDisagree + "\t" + perDisagree + "%" );
resultsJTextArea.append("\n" + "No Opinion" + "\t" + numNuetral + "\t" + perNuetral + "%" );
do
{
if (numAgree < total)
resultsJTextArea.append("\t" + "\t" + "\t" + "*");
if (numDisagree < total)
resultsJTextArea.append("\t" + "\t" + "\t" + "*" );
if (numNuetral < total)
resultsJTextArea.append("\t" + "\t" + "\t" + "*");
} while (numAgree + numDisagree + numNuetral <= total);
} // end method summaryJButtonActionPerformed
// method resets and clears to enable re-start
private void resetJButtonActionPerformed( ActionEvent event )
{
numAgree = 0;
numDisagree = 0;
numNuetral = 0;
total = 0;
responseJTextField.setText( "" );
resultsJTextArea.setText( "" );
summaryJButton.setEnabled(false);
} // end method resetJButtonActionPerformed
// main method
public static void main( String[] args )
{
Survey application = new Survey();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class Survey