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]
# 1

Oops- did not post correctly. here is the code again.

Thanks in advance!

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

sunydee4a at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 2
Is it even invoking that method? Put some System.out.println statements in there temporarily to let you know if it is even running that code.Are you assuming it will magically invoke that method somehow, or did you actually register that method as an event handler (listener)
warnerjaa at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 3

> do

> {

> ... a bunch of stuff that never changes numAgree, numDisagree, numNeutral, nor total

> } while (numAgree + numDisagree + numNuetral <= total);

And yes, that would be an infinite loop if (numAgree + numDisagree + numNuetral <= total), because you never change any of those values in that loop.

warnerjaa at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 4

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

sunydee4a at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 5
I'll assume you didn't see reply #3 before you posted reply #4.
warnerjaa at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 6

Ha- Ha--yes I did, and apparently, I am much slower at this then I thought. <grin>.

I have a listener that invokes the method, I cannot use println within the summary method.

What I do know is that my do while statement leaves much to be desired, and I think that is where the problem is.

Could you possibly suggest another approach for the do while statement, and maybe that will help?

Thanks!

sunydee4a at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 7

> Could you possibly suggest another approach for the

> do while statement, and maybe that will help?

Not really, no. That would require me to know more about your app and be designing it with you, which is much more time-intensive than I would care for.

Basically, I am pointing out that yes you do indeed have an infinite loop, so now you have something to concentrate your efforts on.

If there's nothing going to be changing the condition in that loop, nor any other exit condition provided, then why is it a loop in the first place? You need to think about this design more.

warnerjaa at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...
# 8

Well, I do not think that it is that time consuming, but hey, whatever.

It needs to be a loop because I need an asterik to print out for each response in each of the categories, so it definitely needs to be a loop.

I guess I have stared at it much to long, and am drained with it. Just thought I could get some guidance, not that I want a complete answer.

totally frustrated, but still sunny!!

sunydee4a at 2007-7-13 12:44:46 > top of Java-index,Java Essentials,New To Java...