Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Spinner to Numaric Variable?

Posted on 2006-11-04
9
196 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:kickabury
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17875708
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
0
 
LVL 92

Expert Comment

by:objects
ID: 17875786
> 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;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17875795
You don't need to cast - get the int value as you were doing already, as i quoted above
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 17875891
alternatively if you don't want to cast keep a reference to the model and access it from there.

Number monday = mondaySpinnerModel.getNumber();
0
 

Author Comment

by:kickabury
ID: 17876862
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
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17877506


move that into your formatName method

   protected String formatName() {
        if (!NameSet) return "<<< Start.";

        String firstname = firstnameField.getText();
        String lastname = lastnameField.getText();
        String Sex = (String)SexSpinner.getValue();
        String idnumber = idnumberField.getText();
            Number monday = (Number)mondaySpinner.getValue();
            Number tuesday = (Number)tuesdaySpinner.getValue();
int total = monday.intValue() + tuesday.intValue();
double qAverage = (double)total / 2.0;
        String empty = "";

        if ((firstname == null) || empty.equals(firstname))
            {
            firstname = "<em>(no First Name specified)</em>";
        }
        if ((lastname == null) || empty.equals(lastname))
            {
            lastname = "<em>(no Last Name specified)</em>";
        }

        StringBuffer sb = new StringBuffer();
        sb.append("<html><p align=left>");
        sb.append(firstname);
        sb.append(" ");
        sb.append(lastname);
            sb.append("<br><br>");
        sb.append(idnumber);
            sb.append("<br><br>");
            sb.append(Sex);
            sb.append("<br><br>");
            sb.append(qAverage);
            sb.append("<br><br>");

        return sb.toString();
    }
0
 

Author Comment

by:kickabury
ID: 17877897
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 17879258
use a JLabel for that.
0
 

Author Comment

by:kickabury
ID: 17880953
I put the instructions in the righthand box. It looks and works great. Thanks a million.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 119
login jsp example 24 66
Desingning Refactoring existing code 2 26
SequenceInputStream example 3 19
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question