aspose file tools*
The moose likes Beginning Java and the fly likes Help with an array situation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with an array situation" Watch "Help with an array situation" New topic
Author

Help with an array situation

jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
I want to develop a menu-driven program to maintain information about a group of students using three coordinated arrays. The first array will hold student names, the second their majors, and the third their credits earned. The data for a particular student will be stored at the same index location within each array.
Ok here what I have started code wise
public class App {
// For holding the user's menu selection.
byte choice = 0;
// Constants for testing the user's menu choice.
final byte Student Name = 1;
final byte Major = 2;
final byte Credits Earned = 3;
final byte EXIT = 4;
// This is the main loop. It displays the menu and processes the
user's
// menu choice until they choose to EXIT.
while (choice != EXIT) {
// Draw a separator and display the menu.
Utility.separator(80, '*');
System.out.println("\t" + "Student Data");
Utility.skip();
System.out.println("1 � Student Name");
System.out.println("2 - Major");
System.out.println("3 � Credits Earned");
System.out.println("4 - Exit");
Utility.skip();
System.out.print("Enter your choice: ");
// Read the user's menu selection.
choice = Keyboard.readByte();
// Process the user's selection.
switch (choice) {
case Student_Name
case Major
case Credits_Earned
case Exit
Theses are the other two stipulation s I must follow
1.Change major. When selected, ask the user for the student's name and read it in as a String. Call the find() method (described below) to locate the index of the student within the names array. If found, ask the user for the student's new major and read it in as a String. Call the overloaded store() method (described below) to store the student's major in the majors array at the same index location as the student's name in the names array. .
2.Change credits earned. When selected, ask the user for the student's name and read it in as a String. Call the find() method (described below) to locate the index of the student within the names array. If found, ask the user for the student's new credits earned and read it in as an int. Call the overloaded store() method (described below) to store the student's credits earned in the credits array at the same index location as the student's name in the names array.
Declare three arrays to have 3 elements each. The first must be an array of String object references and have the identifier names. Initialize its elements to reference the values of three names of my choice, such as "bob", "jon", and "sara". The second array must be an array of StringBuffer object references and have an identifier of majors. Its elements should be initially null indicating no major. The third array must be an array of int elements. Name the array credits and let the value of its elements default to zero.
I am Just asking would theses arrays want to declared in each case section and if so, possibly a suggestion on how to start this. Any other suggestion would be greatly appreciated.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Can you code a class that represents a Student?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
Like what do you mean am i aloud to do that or what.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I mean do you know how to code up a class representing a Student, and could you do actually it? Also are you actually allowed to do it to solve this assignment?
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
I figured all of the code would go under the case student section and then i would write my array of a string object. Because I was asked first as stated above that first must be an array of String object reference and have identifier names. Then initialize it elements to reference the values of three students, such as (�bill, Jon, and Sara"). I am just having hard time putting it together. Here what I have again that I started
public class App {
public static void main(String[] args) {
byte choice = 0;
// Constants for testing the user's menu choice.
final byte Student_Name = 1;
final byte Major = 2;
final byte Credits_Earned = 3;
final byte EXIT = 4;
// This is the main loop. It displays the menu and processes the user's
// menu choice until they choose to EXIT.
while (choice != EXIT) {
// Draw a separator anpublic class App {

while (choice != EXIT) {
// Draw a separator and display the menu.
Utility.separator(80, '*');
System.out.println("\t" + "Student Data");
Utility.skip();
System.out.println("1 � Student Name");
System.out.println("2 - Major");
System.out.println("3 � Credits Earned");
System.out.println("4 - Exit");
Utility.skip();
System.out.print("Enter your choice: ");
// Read the user's menu selection.
choice = Keyboard.readByte();
// Process the user's selection.
switch (choice) {
case Student_Name:
String Student = (Keyboard.readString());
char Student = { Bill, Jon, Sara };

case Major:
case Credits_Earned:
case EXIT:
}

}
}
}
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
So far, the code looks like you are headed in the right direction. It sounds like you can't create a Student class and then use an array of student objects, which is what Barry was getting at. Instead, you need to use three "parallel arrays" to store the information. This shouldn't be too difficult for you to add to the current program you have. The first step is to actually declare these arrays and initialize them. Can you do that much at least?
I'll check back later to see what you come up with.
Layne
p.s. Please use the CODE tags when you post your code. They will preserve the format of your code, which makes it easier for us to read it. You can simply click the CODE button below the text area where you enter your message. Then paste the code in between the automatically generated CODE tags.


Java API Documentation
The Java Tutorial
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
which is what Barry was getting at.

I was not thinking of making an array of Students.
Something completely different in fact
I was wanting to get to a solution that is a bit more OO like.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53

Does this set up array correctly?
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
Well I want an array for students, then another for major, and another one for Credits earned. That what I was wandering can that be declared under each Case I have.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
Any help
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Barry Gaunt:

I was not thinking of making an array of Students.
Something completely different in fact
I was wanting to get to a solution that is a bit more OO like.

Okay, I guess I was jumping the gun a bit. I'm sure the discussion would have lead to arrays of object, eventually. However, it looks like that isn't even required for this program.
Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
No, the declarations should not go under each case. And the arrays should not be initialized to static values as you did above. Instead, you need the user to enter the value and then store that into the array. You should declare the arrays at the top of main() and then the case statements actually put the information that the user enters into the appropriate array.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
What would array look like then I guess I am just really lost on how to start this. this confusing to me.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
so beofr the switch section if I put
char[] student = new int[4];
would that be all to create the array I need. Then I could go down to the case statement and declare the names in a string.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Well, it looks close to what you have already. The biggest problem is where you have it located. First, you need to change the declaration to something like this:

I am assuming a maximum of 10 students here, but you can change it to any number you wish. These two lines go at the top of main(). You can also declare the other arrays in a similar manner.
HTH
Layne
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Layne said:
I'm sure the discussion would have lead to arrays of object, eventually.

Nope.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by jon ladd:
char[] student = new int[4];

You have the right idea, but you can't mix types like this. If you declare an array of char, you need to initialize it with an array of char, not an array of int.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Layne said:
Hehe, I guess that's what I get for assuming.

Yup. (In Meaningless Drivel that would have you ***insert something very, very nasty here*** )
Ok, I did (mis)lead you on a bit... but you made an important discovery.
Layne, Suppose jon had a class Student representing a student. He would then need a class Klass (horrible name) to represent a school class. Klass would have a constructor Klass(int capacity) and methods for adding and removing students Klass.add(Student), Klass.remove(Student), Klass.list(), Klass.find(Student), Klass.modify(Student) and so on. Now the assignment specifies the implementation of Klass as using three arrays, can't get away from that. But all the gory details can be hidden away in that class Klass:

As I said I was trying to get to a more OO-like solution. All neat and tidy like...
About 100 of these edited things:
[ March 25, 2003: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Finally I got it done. But somehow I lost Laynes post that I quoted. Sorry, I screwed up. I wish I could disable these superpower bartender privileges sometimes.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53

Ok I figured out I have declared my Arrays. Now under case Change of major it prompts you to enter for one of three names I declared in the array. So now I want to be able display the name of the student and be able to enter a major for them. The problem I am running in to is when it asks for the name it can�t find it. Here is the code that I am using.
name = student.indexOf(find, name);
then it gives me all kinds of errors.
Any suggestion on how to delcare this find?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I think you are confusing your array with your String here.
The method "indexOf" is a feature of the String class. But you are trying to use it on "student" which is an array of Strings.


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Another problem, assuming you were using indexOf on the correct varible, is that you are not using this method correctly.
You are using "find" as the first parameter to this method, but I don't see where "find" is actually defined anywhere. In addition the line

is just a little confusing, to me atleast. In the first place it isn't the name, it is the first character of the name. So the name of the variable isn't helpful.
Than you are assigning zero to a char variable. Why do that? It can be done but it just makes your code confusing for no good reason.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

To be able to find a specific element in an array, in your case the name of a student, you need to step through the array checking each element in the array. The most common way of doing this is with a 'for' loop.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by jon ladd:
Ok I figured out I have declared my Arrays. Now under case Change of major it prompts you to enter for one of three names I declared in the array. So now I want to be able display the name of the student and be able to enter a major for them. The problem I am running in to is when it asks for the name it can�t find it. Here is the code that I am using.
name = student.indexOf(find, name);
then it gives me all kinds of errors.
Any suggestion on how to delcare this find?

William has pointed out all the issues with the above line of code. However, I'd like to point you in the correct direction with some syntax. Remember that the variable "student" is an array. Therefore, you simply have to use an index to get a specific name out of the array:

This gives the name of the second student in the array (remember that arrays start at index zero). Also, since student is an array of String, then the above is now just a String. This means that you can use it wherever you would use a simple String variable (e.g. a println() call, hint, hint).
I hope that answers your current question(s). I also have a suggestion/comment about your code. I'm puzzled why you are using an array of StringBuffer for the majors. StringBuffer is used when the String itself needs to be modified (e.g. inserting, appending, or deleting characters from a specific String). However, in this case, I *think* you simply need to replace one string with another when the major changes. If this is the way it should be, it would be easier to use an array of String.
All in all, you seem to coming along pretty well with this program. Please come back with more questions.
Keep coding!
Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with an array situation
 
Similar Threads
storing an array in a meathod problem...
Printing out of an data stored in and array and SringBuffer array
Need Help with Indexes
Need some urgent help
I cannot fix this program! if anyone can help!