my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
In an assignment I have been asked to accept a user input for student name and student course using JOptionPane.showInputDialog("enter name");
JOptionPane.showInputDialog("enter course");
I need 24 names and courses....My question is can I store both name and course in the same array? If so how do I do it?

Alan.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
If you need to use arrays, you have two choices:

1. Create a class to hold both fields, then add objects of that class to the array;

2. Create two parallel arrays, one for each field. When you save the data or retrieve the data, you use the same index for both arrays.

Hope that helps.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Alan Arkwell
Greenhorn

Joined: Apr 21, 2005
Posts: 10
Jeff,you say if you need to use an array.Is there a better way of storing the info?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
It helps to know the current learning goals for the assignment. If you're supposed to learn about arrays, then using them is great for this assignment. If you are free to use the Java Collections, there are quite a few that would work here.

Having seen this assignment here a lot in the past few months, I'm guessing the intention is to have you use arrays, but I could be mistaken.

There's a third, in my view least-desirable, option: since they're both Strings, use a single array and alternate student name and course name (a.k.a. the C way ).
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
David -

I'm still a C programmer, and I would use the parallel array method before I'd alternate. It just seems cleaner.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
By "the C way" I only meant that I haven't had to resort to that class of technique since my C coding days. In other words, when I was first learning to code.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Ah.

In my first year of Java programming, I basically treated Java as a revised C. I look back on my old code and cringe!
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Originally posted by Jeff Bosch:
Ah.

In my first year of Java programming, I basically treated Java as a revised C. I look back on my old code and cringe!


If you wish to become a Java convert, you should be happy to make the transition from procedural programming to OOP. Using the 1st method is much better because it is a good OO solution and very extensible.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
If you wish to become a Java convert, you should be happy to make the transition from procedural programming to OOP.


Actually, I still use procedural quite a lot in small-footprint embedded systems. I find that both paradigms are akin to screwdrivers: we have specific, appropriate screwdrivers for phillips, slotted, hex, torx, etc. It all depends on the requirements.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Personally, I like Jeff's initial suggestion. You can create a Student class that holds the information for a single student. Then you can use an array several Student objects that are, for example, in the same class.

Layne


Java API Documentation
The Java Tutorial
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I offered both options because it's possible that the course has not yet taught how to create classes. I also prefer the class option, but maybe he's not up to that level yet. Arrays seem to come before classes in the courses I've seen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays
 
Similar Threads
while loop
hashing problem
Control for String
Hashing Problem
Hashing Storage