How can I store variables in an array. With a mixture of strings, variables and information entered in textfields. With what I have below. If I wanted to store numbers, price, total, tax, and name in an array, how can I do it? Thanks in advance.

String[] numbers = { "1","2", "3", "4", "5", "6" };

double price = 50.0;
double total = 100.0;

JComboBox tax = new JComboBox(numbers);
JFormattedTextField name = new JFormattedTextField();

Recommended Answers

All 4 Replies

I assume each of those attributes are related. If so, you should probably encapsulate that information into an object, and then store those objects in an array.

How can I go about doing that. I've been searching and I'm currently unable to find the answer. I'm familiar with setting arrays of the same type, but when they're different, that's where I'm lost.

I don't see why you would want to create an array containing different data types, and as far as I know, you can't.

What I assume is that you are trying to implement some sort of store where products have a price, tax etc. This would mean this information needs to be kept tied together and an array is not the best way to do this. Use an object (new class) where you store the price, tax, etc (if you store it as an object, the data types are completely up to you and you are essentially creating your own data type).

After you do that, all you need to do is create an array of your type.

Is that clearer?

I understand it better now. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.