File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Arrays and strings---a little help, please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays and strings---a little help, please" Watch "Arrays and strings---a little help, please" New topic
Author

Arrays and strings---a little help, please

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
Hello!
Could you look over my program? The specifications for how it should work are in the description in the program statement. I can't get the student and status to print out. Do I need another array? My program is as follows:
/*Student.java--this program will create a class for students that will contain the studnet's name(string), the student's ID(int) and status(int). The status will be assigned anumber corresponding to the student's class status: 1,freshman; 2, sophomore; 3, junior; 4, senior. The program will use the Math.random() function to assign the student ID and status randomly. The program will output 1 list of all students, then another list that will print out only those of junior(3) status.*/

public class Student
{
//=== class variables ====
String studentName;
int studentID;
int studentStatus;
//=== Constructors ===
public Student()
{
studentID = (int)( Math.random() * 10000 );
studentStatus = (int)(Math.random() * 4 + 1);

}
public Student(String sName)
{ this();
studentName = sName;
}
public static void main(String args[])
{

Student [] studentBody; // create a reference to an array of Student
studentBody = new Student[20]; // create the array of 20 references to type Student
// populate the array with 20 Students
for (int i = 0; i < studentBody.length; i++)
{
studentBody[i] = new Student("Mary", "Joe", "Susan", "Shannon", "Declan", "Judith", "Simeon"
"Regina", "Dean", "Kathleen", "Eileen", "Bruce", "Catharine", "Louise", "Stephen", "Brian", "Richard", "Foster", "Jean", "Fauna"); }
// print out the contents of studentBody
System.out.println("The entire student body:\n");
for (int i = 0; i < studentBody.length; i++)
{
System.out.println("Student: " + studentBody[i].studentName + "\n" + " ID: "+ studentBody[i].studentID + "\n" + " status: "+ studentBody[i].studentStatus + "\n");
}
}
}
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
The way you are populating your studentBody array does not look like what you want to do. I assume this is a class assignment, so I won't give you the answer directly. You are on the right track that you need another array containing the names of the students, you can then call your constructor that takes the name of the student for each element in your array.
In terms of coding style, I would also have separate methods for populating the array and for each of the prints.
Hope this helps.
Graeme
huiying li
Ranch Hand

Joined: Feb 12, 2001
Posts: 68
Here is a hint to assign a list of names to an array of Strings use the following syntax.
String[] studentNames = {"Mary", "Joe", "Susan"};


SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays and strings---a little help, please