Link to home
Start Free TrialLog in
Avatar of kickabury
kickabury

asked on

Spinner to Numaric Variable?

I have been working on a GUI Java program for a course I'm taking and I can't get a spinner to be treated as a numaric value so I can add it up and display it. I would ask my teacher, but he hardly speaks english and I usually come out more confused than when I started. So, please, please, please help be my teacher.

Here is the prob, I need the mondaySpinner and the tuesdaySpinner to add up and then average and display. I have left out the average, and the other days of the week, because I first wanted to see if I could get any number out of the spinners before going further. And, I know the code is really messy, but I get graded on whether it works, and the teacher doesn't look at the structure of the code.

Look at the Name.java file in this zip file: http://www.instantinfo.net/Name.zip

Thanks in advance,

James
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You were going in the right direction:


>>mondaySpinner.getNumber().intValue()

but i would make Name implement ChangeListener and add it as a listener to both spinners. Each time there's a change, calculate the total and increment the number of changes that have occurred. When you're ready

avg = total / numberOfChanges
> Number monday = mondaySpinner.getValue();

u were close, just need to cast

Number monday = (Number) mondaySpinner.getValue();

then do the same with tuesday

Number tuesday= (Number) tuesdaySpinner.getValue();

and sum:

int sum = monday.intValue() + tuesday.intValue();

and average:

double average = (double) sum / 2.0;
You don't need to cast - get the int value as you were doing already, as i quoted above
alternatively if you don't want to cast keep a reference to the model and access it from there.

Number monday = mondaySpinnerModel.getNumber();
Avatar of kickabury
kickabury

ASKER

Thanks for the quick responses. I couldn't get the mondaySpinner.getNumber().intValue() to compile without erroring. Not sure why, but Number monday = (Number) mondaySpinner.getValue(); works. I was able to make "monday" display, but I can't get the average to work and display.

Here is the new zip file: http://www.instantinfo.net/Name2.zip
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh. Nice. It works great. Thank you so much.

Before I close this out, one more quick question. I should probably add some simple directions like "Please enter the number of sales for each day of the week." I tried to add a JTextArea right above the mondaySpinner, it compiles, then spits out all kinds of crazy messages when I try to run it. Can I easily add a line of text, or should I leave good enough alone?

Thanks again.
use a JLabel for that.
I put the instructions in the righthand box. It looks and works great. Thanks a million.