Hi there, I am trying to display three Attributes assigned to each "Student" which variables are defined in my 'Student' Class. In my show student class, I can get the user to decide how many Students they would like to add, and i also get the user to input the variables for Name, StudentID and Address. However I would like to display them in the format of void display() in the 'Student Class'.
I know i will have to use a for statement and i am trying to print out the contents of the array that i have stored.
Here is my code:
class Student
{
public String Name;
public String StudentID;
public String Address;
//constructor
public Student(String SName, String SID, String SAdd)
{
Name = SName;
StudentID = SID;
Address = SAdd;
}
//get methods
public String getName()
{
return Name;
}
public String getID()
{
return StudentID;
}
public String getAddress()
{
return Address;
}
//set method
public void setName( String SName)
{
Name = SName;
}
public void setID (String SID)
{
StudentID = SID;
}
public void setAddress (String SAdd)
{
Address = SAdd;
}
//display method
public void display()
{
System.out.println(Name + "\n" + StudentID +
"\n" + Address + ".");
}
}
And my Show Student Class:
import java.util.*;
import javax.swing.*;
import java.util.Scanner;
public class ShowStudent1
{
public static void main(String[] args)
{
// variables from Student
String Name;
String StudentID;
String Address;
// asking the user how many they would like to add
int choice;
Student[] StArray;
System.out.print("How many new Students are you going to add: ");
Scanner in = new Scanner(System.in);
choice = Integer.parseInt(in.nextLine());
//creating student array of chosen amount
StArray = new Student[choice];
//asking user for the variables and trying to display them?
for (int x = 0; x < StArray.length; x++)
{
Name = JOptionPane.showInputDialog("Enter First and Last Name:");
StudentID = JOptionPane.showInputDialog("Enter StudentID");
Address = JOptionPane.showInputDialog("Enter Adress");
Student[] Object = new Student[x];
StArray[x] = Object[x];
System.out.println(Object[x]);
}
}
}
Thank you