0

I am completely new to Java and have an assignment coming up; with the brief being the following:

A Swing interface. The application must allow the user to enter some data and click a button. The application must have an event handler to react to the click button event. The application must perform some operation on the data entered. The application must return the modified data to the user.

I was originally going to create an app that converted numbers to different units (eg. kgs to lbs) but found it very difficult, so have decided to go for an app which finds the average of a number of inputs.

This is the code i have for finding the average of a pre-defined array:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

    class FindAverageNumber extends JFrame {
        public static void main(String[] args){
            double numbers[] = new double[]{1,2,3,4,5,6,7,8,9,20};
            double average = 0;
            double cumulative = 0;
            for (int i=0; i<numbers.length; i++){
                double selected = numbers[i];
                cumulative = (cumulative + selected);
            }
            average = (cumulative / numbers.length);
            System.out.println("The average of the array is: "+average);
        }
    }

The code seems to function properly but, being new to Java, i don't know how to take in these numbers from the user while also incorporating a swing interface (which i'm lost on). I'm assuming accepting an array is the best way to do this, with the user being presented with a textfield. I don't know how to separate the inputs up into their own index in the array.

I'd appreciate any help; i realise i have a lot of basic questions.

3
  • If you need to make a swing, you should look into Netbeans IDE. They have a good swing maker where you would just drag and drop the components you need, create the event handler, etc. If you don't mind me asking, what did you find difficult about the unit conversion question? Was it math related or were you having problems with the swing aspect like you are now?
    – Mitch
    Jan 3, 2012 at 17:15
  • I actually am using Netbeans. Like Java, i'm very new to it. My problem with the converter was i think it is just over my head - adding things like a dropdown list to select things like weight, length, time, temperature etc....another for the specific unit to convert from, kg, lb, sec, hrs etc....a textfield for the user to input the quantity, another dropdown list for the unit to convert to. These are all things i don't know how to do. I feel i could write out the pseudocode with no problem but i'm pretty useless when it comes to the proper coding
    – garethdn
    Jan 3, 2012 at 17:27
  • See also this example.
    – trashgod
    Jan 3, 2012 at 17:52

5 Answers 5

4

If you have a text field, you'll need to parse the input. You can use the String.split() method:

For example, if you received the input as "1,2,3,4,5" you would call it like

input.split(",");
2
  • 1
    It is best to link to the latest version of the JavaDocs. I have edited your answer to point to J2SE 7. For tips on getting a link to the latest docs, see point 2 of advantages. Jan 3, 2012 at 17:21
  • 1
    String.split() is a good suggestion, but be very careful to then call trim() on the pieces to remove any leading or trailing spaces the user may have typed. And then check for empty Strings in case the user mistakenly typed two commas in a row. These are two common pitfalls with split().
    – user949300
    Jan 3, 2012 at 17:21
2

Instead of trying to accept an array, it would go for a UI containing a JTextField for input of a single number and a JButton to submit the input. You can then update the average/total/... (whatever you want to calculate) based on the numbers you already processed. If the user wants to add an extra number, it just fills in an extra value and presses the button again. Much more friendly to the user then having to input an array, and less possibilities to get confused (separate numbers by spaces, semicolons, comma's, ... )

To display the calculated values you can use a JLabel (or multiple ones).

To get you started on UI building you can take a look at the excellent Swing tutorial. The keywords to search for or the classes I already mentioned, and JFrame and JPanel as container to add all those components to

2
  • I think i may try this out. It seems to make the most sense to me. Thanks
    – garethdn
    Jan 3, 2012 at 17:55
  • @mKorbel level 1 support experience pays off. Not just answering the question, but trying to guide the customer in the correct direction to avoid further support in the future, where things have become one big ugly mess
    – Robin
    Jan 3, 2012 at 17:58
0

You can take the user input as a string delimited by some character like ,(comma) or :(colon). Once you have the input you can split in numbers and do the computation.

Note: You being a student, I don't want to give the exact program to do this. Explore on your own and if again you have some difficulty you can ask questions.

0

Have a look at any Swing tutorial. Specifically, look at JFrame, JTextField, JButton. You should also read up on what MVCs are. In this case, your model consists of the numbers entered (or some function there-of). So you can choose to avoid comma separated strings and just expose an Add button. If you do go the route of allowing only one number to be entered at a time, look at JFormattedTextField paired with a DecimalFormat

Also, from a mathematical point of view, if you are keeping track of an indefinite amount of numbers, you should consider using BigDecimal. Note that it has a constructor that takes a string.

0

Go google "Java Swing Tutorial." There will be several options to choose from on how to display UI controls the user.

http://www.javabeginner.com/java-swing/java-swing-tutorial

There is even code for showing a JTextField on a JFrame in there. You should be able to figure out how to add a JButton to that mix as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.