Need help with Logic Error(I think?)

Hello. The following is a program I am creating to calculate the start value of a gymnastics routine, the basics are as follows(And not everything is implemented yet):

In gymnastics there are the aparatuses, then skill group codes, then "skills" a-g or something like that(My brother is the gymnast, i'm the programmer). Basically as of right now im testing the logic of actually adding up the values based on which option they choose, since the options are strings, i set the strings into an array and then i have a score value array(Values are random at this point).

The problem is, the totalvalue(i think its called that) number is not showing up. There is something wrong in the way i order the things.

This compiles PERFECTLY and runs, and everything is right except the showing up of a number which should show when you choose options in the combo boxes.

Here is the code, help please :)!!!

Main:

//

//GymnasticsMain.java

//The Main top program - calls the panels.

//By: Ian Coolidge

//June 5, 2007

//

import javax.swing.*;

publicclass GymnasticsMain

{

//Calls all the panels, they can be found under their names

publicstaticvoid main (String[] args)

{

JFrame frame =new JFrame ("Gymnastics Super Program");

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

GymnasticsControls controlPanel =new GymnasticsControls();

frame.getContentPane().add(controlPanel);

frame.pack();

frame.setVisible(true);

}

}

The Guts of the program:

//--

//GymnasticsCrontrols.java

//This creates the actual guts of the program, you know, the buttons and menus and the such

//By: Ian Coolidge

//June 5, 2007

//--

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.AudioClip;

import java.net.URL;

import java.lang.*;

publicclass GymnasticsControlsextends JPanel

{

private JComboBox GymApparatus;

private JComboBox GymGroupCode;

private JLabel label;

private JLabel scoreLabel;

privatedouble currentApparatus;

privatedouble currentCode;

private String ScoreLine;

privatedouble TotalValue;

privatedouble[] score =newdouble[20];

public GymnasticsControls()

{

score[0] = 1.2;

score[1] = 1.4;

score[2] = 0.5;

score[3] = 0.2;

score[4] = 2.2;

score[5] = 0.7;

score[6] = 0.2;

score[7] = 0.2;

score[8] = 6.2;

score[9] = 3.2;

score[10] = 2.2;

score[11] = 0.76;

score[12] = 0.378;

score[13] = 0.87;

score[14] = 0.19784;

score[15] = 0.1278;

score[16] = 0.2834;

score[17] = 0.3534;

score[18] = 0.323;

score[19] = 0.45;

ScoreLine ="Your starting value is: "+TotalValue;

scoreLabel =new JLabel(ScoreLine);

//The following is the declaration of the list of options in the drop down menus

String[] Apparatus ={"Apparatus","Floor","Pommel","Vault","Rings","PBars","High Bar"};

GymApparatus =new JComboBox (Apparatus);

GymApparatus.setAlignmentX (Component.LEFT_ALIGNMENT);

String[] GroupCode ={"Group Code","I","II","III","IV","V"};

GymGroupCode =new JComboBox (GroupCode);

GymGroupCode.setAlignmentX (Component.LEFT_ALIGNMENT);

//Sets up the panel

add (scoreLabel);

setPreferredSize (new Dimension (300, 100));

setBackground (Color.cyan);

setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));

add (Box.createRigidArea (new Dimension(0,5)));

add (GymApparatus);

add (Box.createRigidArea (new Dimension(0,5)));

add (GymGroupCode);

add (Box.createRigidArea (new Dimension(0,5)));

GymApparatus.addActionListener (new ComboListener());

GymGroupCode.addActionListener (new ComboListener());

TotalValue = currentApparatus + currentCode;

}

privateclass ComboListenerimplements ActionListener

{

publicvoid actionPerformed (ActionEvent event)

{

//Should assign a number based on the number string to whatever is chosen in the drop down menu

currentApparatus = score[GymApparatus.getSelectedIndex()];

currentCode = score[GymGroupCode.getSelectedIndex()];

}

}

}

I tried moving stuff all around, and it still doesn't work. Thanks very much for all the help!

-Ian

[7432 byte] By [Stemmana] at [2007-11-27 6:39:48]
# 1

modified your combo listener actionPerformed method to this:

//Should assign a number based on the number string to whatever is chosen in the drop down menu

currentApparatus = score[GymApparatus.getSelectedIndex()];

currentCode = score[GymGroupCode.getSelectedIndex()];

// Add this (actually moved from constructor).

TotalValue = currentApparatus + currentCode;

ScoreLine = "Your starting value is: "+TotalValue;

scoreLabel.setText(ScoreLine);

Message was edited by:

j_shadinata

j_shadinataa at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 2
Your first use of TotalValue is null. You add your sentence "Your starting value is" + null.If your programming is just showing "Your starting value is" then that's your problem. Then later in the program, TotalValue changes, but you never change the JLabel.
lethalwirea at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 3
> Your first use of TotalValue is null. You add your sentence "Your > starting value is" + null.No. TotalValue is double. It will result "Your starting value is 0".
j_shadinataa at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 4

TotalValue = currentApparatus + currentCode;

This is not dynamic. It gets executed once not everytime the currentApparatus and/or currentCode variables change.

Perhaps you need to move this line into the actionPerformed method. However, there is no code that I can see that will

display this value if you managed to calculate it correctly.

Jeebus. How slow am I?

Message was edited by:

flounder

floundera at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 5

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.AudioClip;

import java.net.URL;

import java.lang.*;

public class GymnasticsControls extends JPanel

{

private JComboBox GymApparatus;

private JComboBox GymGroupCode;

private JLabel label;

private JLabel scoreLabel;

private double currentApparatus;

private double currentCode;

private String ScoreLine;

private double TotalValue;

private double[] score = new double[20];

public GymnasticsControls()

{

score[0] = 1.2;

score[1] = 1.4;

score[2] = 0.5;

score[3] = 0.2;

score[4] = 2.2;

score[5] = 0.7;

score[6] = 0.2;

score[7] = 0.2;

score[8] = 6.2;

score[9] = 3.2;

score[10] = 2.2;

score[11] = 0.76;

score[12] = 0.378;

score[13] = 0.87;

score[14] = 0.19784;

score[15] = 0.1278;

score[16] = 0.2834;

score[17] = 0.3534;

score[18] = 0.323;

score[19] = 0.45;

ScoreLine = "Your starting value is: "+TotalValue;

scoreLabel = new JLabel(ScoreLine);

//The following is the declaration of the list of options in the drop down menus

String[] Apparatus = {"Apparatus", "Floor", "Pommel", "Vault", "Rings", "PBars", "High Bar"};

GymApparatus = new JComboBox (Apparatus);

GymApparatus.setAlignmentX (Component.LEFT_ALIGNMENT);

String[] GroupCode = {"Group Code", "I", "II", "III", "IV", "V"};

GymGroupCode = new JComboBox (GroupCode);

GymGroupCode.setAlignmentX (Component.LEFT_ALIGNMENT);

//Sets up the panel

add (scoreLabel);

setPreferredSize (new Dimension (300, 100));

setBackground (Color.cyan);

setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));

add (Box.createRigidArea (new Dimension(0,5)));

add (GymApparatus);

add (Box.createRigidArea (new Dimension(0,5)));

add (GymGroupCode);

add (Box.createRigidArea (new Dimension(0,5)));

GymApparatus.addItemListener(new ComboListener());

GymGroupCode.addItemListener(new ComboListener());

TotalValue = currentApparatus + currentCode;

}

private class ComboListener implements ItemListener

{

public void itemStateChanged(ItemEvent event)

{

//Should assign a number based on the number string to whatever is chosen in the drop down menu

currentApparatus = score[GymApparatus.getSelectedIndex()];

currentCode = score[GymGroupCode.getSelectedIndex()];

TotalValue = currentApparatus ;

scoreLabel.setText("Your starting value is: " + TotalValue);

}

}

}

Check the above modified code. Its an example. ItemListener added instead of ActionListener

AnanSmritia at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 6
This worked!! Thank you soo much!(I used the first post's advice, thanks!)I'm sure the last post would have also worked.I'll be updating it, you'll hear back if it messes up again.Thanks a ton!Message was edited by: Stemman
Stemmana at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 7

> > Your first use of TotalValue is null. You add your

> sentence "Your

> > starting value is" + null.

>

> No. TotalValue is double. It will result "Your

> starting value is 0".

Psshhh, I'm not thinking today. :D I guess I should start gaining more sleep.

lethalwirea at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...
# 8
I'm sure you've heard this before, but consider separating your program logic from the UI. The logic should be set up such that it would work well with a GUI like Swing or a console interface. That way you have greater flexibility, great upgradability and an easier time debugging.
petes1234a at 2007-7-12 18:09:02 > top of Java-index,Java Essentials,Java Programming...