Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java help: problem getting counter and sum to accumulate
New Posts  All Forums:Forum Nav:

Java help: problem getting counter and sum to accumulate

post #1 of 3
Thread Starter 
Having a few issues as the title states. My sum(total) is not accumulating and my count is not increasing. Along with that my worst and best scores are not storing as well. They seem to be resetting each time I run the calculation.

Any help would be appreciated.
Code:
import java.awt.event.*;
import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;

public class scores{
        public static void main(String args[]){
                //construct the frame
                JFrame frame = new StudentScoresFrame();
                frame.setVisible(true);
        } //end main
} //end StudentScoresApp class

class StudentScoresFrame extends JFrame{
        public StudentScoresFrame(){
                //frame properties
                setTitle("Test Scores");
                setSize(250, 210);
                setResizable(false);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //construct the panel
                JPanel panel = new StudentScoresPanel();
                add(panel);
        } //end StudentScoresFrame construct
} //end StudentScoresFrame class

class StudentScoresPanel extends JPanel implements ActionListener{

        //declare buttons, text fields, and labels
        private JButton enterButton, clearButton, exitButton;
        private JTextField scoreField, numberField, averageField, bestField, worstField;
        private JLabel scoreLabel, numberLabel, averageLabel, bestLabel, worstLabel;

        public StudentScoresPanel(){

                //set the layout for the StudentScoresPanel
                setLayout(new BorderLayout());

                //construct a panel for the display
                JPanel displayPanel = new JPanel();
                displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

                //construct and add labels and text fields to the panel
                scoreLabel = new JLabel("Test score:");
                displayPanel.add(scoreLabel);

                scoreField = new JTextField(10);
                scoreField.addActionListener(this);
                displayPanel.add(scoreField);

                numberLabel = new JLabel("Number of scores:");
                displayPanel.add(numberLabel);

                numberField = new JTextField(10);
                numberField.setEditable(false);
                numberField.setFocusable(false);
                displayPanel.add(numberField);

                averageLabel = new JLabel("Average score:");
                displayPanel.add(averageLabel);

                averageField = new JTextField(10);
                averageField.setEditable(false);
                averageField.setFocusable(false);
                displayPanel.add(averageField);

                bestLabel = new JLabel("Best score:");
                displayPanel.add(bestLabel);

                bestField = new JTextField(10);
                bestField.setEditable(false);
                bestField.setFocusable(false);
                displayPanel.add(bestField);

                worstLabel = new JLabel("Worst score:");
                displayPanel.add(worstLabel);

                worstField = new JTextField(10);
                worstField.setEditable(false);
                worstField.setFocusable(false);
                displayPanel.add(worstField);

                //construct a panel for the buttons
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

                //construct and add buttons to the panel
                enterButton = new JButton("Enter score");
                enterButton.addActionListener(this);
                buttonPanel.add(enterButton);

                clearButton = new JButton("Clear");
                clearButton.addActionListener(this);
                buttonPanel.add(clearButton);

                exitButton = new JButton("Exit");
                exitButton.addActionListener(this);
                buttonPanel.add(exitButton);

                //add panels to the main panel
                add(displayPanel, BorderLayout.CENTER);
                add(buttonPanel, BorderLayout.SOUTH);

        } //end StudentScorePanel() constructor

        public void actionPerformed(ActionEvent e){

                double average = 0.0;
                double best = Double.parseDouble(scoreField.getText());
                double worst = Double.parseDouble(scoreField.getText());
                double scores = Double.parseDouble(scoreField.getText());
                double sum = 0.0;
                int count = 0;
                String choice = "";

                Object source = e.getSource();

                if(source == enterButton){

                        sum =+ scores;
                        count ++;
                        average = sum/count;

                        numberField.setText("" + count);
                        averageField.setText("" + average);

                        if(scores > best){
                                best = scores;
                        }
                        if(scores < worst){
                                worst = scores;
                        }

                        bestField.setText("" + best);
                        worstField.setText("" + worst);

                        scoreField.requestFocus();
                        scoreField.selectAll();
                } //end if
                else if(source == clearButton){
                        scoreField.setText("");
                        numberField.setText("");
                        averageField.setText("");
                        bestField.setText("");
                        worstField.setText("");
                        scoreField.requestFocus();
                } //end else if
                else if (source == exitButton){
                        System.exit(0);
                } //end else
        } //end actionPerformed
}  //end StudentScoresPanel class

Gnomeon
(16 items)
 
GnomeBook Pro
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1245 V2 ASUS P8Z77-V SAPPHIRE TRI-X OC Radeon R9 290X 4GB  Crucial Ballistix Tactical 16GB DDR3 1866 
Hard DriveHard DriveCoolingOS
Crucial M550 256GB HGST Travelstar 7K1000 2.5-Inch 1TB 7200 RPM Cooler Master Hyper 212 Evo Windows 10 Pro 64-Bit 
MonitorKeyboardPowerCase
BenQ XL2730Z Cooler Master Quickfire Pro Unbranded (Tenkeyless) SeaSonic SG12 550W Fractal Design Define S 
MouseAudioAudioOther
Mionix Avior Swan M10 Philips Fidelio X1 Audio-Technica ATR2500-USB 
CPUMotherboardGraphicsRAM
Intel Core i7-2720QM Logic Board Intel HD Graphics 3000 and AMD 6750M 1gb Crucial 16GB 1333MHZ 
Hard DriveOptical DriveOSMonitor
750gb Hitachi 5400rpm 8x double-layer SuperDrive OS x 10.7.4 15.4" LED-backlit glossy widescreen display 144... 
KeyboardPowerCaseMouse
Apple Keyboard Apple 85W MagSafe Power Adapter 2011 Unibody Magic Trackpad 
Audio
Built-in Stereo Speakers with Subwoofer 
  hide details  
Reply
Gnomeon
(16 items)
 
GnomeBook Pro
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1245 V2 ASUS P8Z77-V SAPPHIRE TRI-X OC Radeon R9 290X 4GB  Crucial Ballistix Tactical 16GB DDR3 1866 
Hard DriveHard DriveCoolingOS
Crucial M550 256GB HGST Travelstar 7K1000 2.5-Inch 1TB 7200 RPM Cooler Master Hyper 212 Evo Windows 10 Pro 64-Bit 
MonitorKeyboardPowerCase
BenQ XL2730Z Cooler Master Quickfire Pro Unbranded (Tenkeyless) SeaSonic SG12 550W Fractal Design Define S 
MouseAudioAudioOther
Mionix Avior Swan M10 Philips Fidelio X1 Audio-Technica ATR2500-USB 
CPUMotherboardGraphicsRAM
Intel Core i7-2720QM Logic Board Intel HD Graphics 3000 and AMD 6750M 1gb Crucial 16GB 1333MHZ 
Hard DriveOptical DriveOSMonitor
750gb Hitachi 5400rpm 8x double-layer SuperDrive OS x 10.7.4 15.4" LED-backlit glossy widescreen display 144... 
KeyboardPowerCaseMouse
Apple Keyboard Apple 85W MagSafe Power Adapter 2011 Unibody Magic Trackpad 
Audio
Built-in Stereo Speakers with Subwoofer 
  hide details  
Reply
post #2 of 3
Are you in Hileman's class at SIUE?
Ironhide
(15 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASUS P8Z77-V LE MSI GTX 465 Twin Frozr II GE G.SKILL Ripjaws 8Gb 
Hard DriveHard DriveOptical DriveOS
Crucial SSD Western Digital Black LG Blu Ray Burner Win 7 x64 Home Premium 
KeyboardPowerCaseMouse
Ducky Shine Browns Corsair 750HX CM 690 II Logitech G500 
Audio
Creative X-Fi Titanium HD 
  hide details  
Reply
Ironhide
(15 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASUS P8Z77-V LE MSI GTX 465 Twin Frozr II GE G.SKILL Ripjaws 8Gb 
Hard DriveHard DriveOptical DriveOS
Crucial SSD Western Digital Black LG Blu Ray Burner Win 7 x64 Home Premium 
KeyboardPowerCaseMouse
Ducky Shine Browns Corsair 750HX CM 690 II Logitech G500 
Audio
Creative X-Fi Titanium HD 
  hide details  
Reply
post #3 of 3
ok, when you declare those doubles in your action performed method, you are reinitializing them to zero every time. that's why your program never stores the values or increments the count. you need to make them all class variables. you also need to reset their values when the clear button is pressed, not just resetting the fields.

also, sum needs to plus-equal scores, not the other way around.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java help: problem getting counter and sum to accumulate