Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays and strings---a little help, please

 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a hint to assign a list of names to an array of Strings use the following syntax.
String[] studentNames = {"Mary", "Joe", "Susan"};

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic