Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Alan Arkwell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alan Arkwell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,you say if you need to use an array.Is there a better way of storing the info?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic