How to fix my Array class - java

I am trying to figure out how to get my array to run correctly, I know I have to change the array value to an input but I cannot get the program to compile if any one can help that be great.
I am trying to have the program take input for grades and names of students and in the end output their name and grade.
Edit sorry this is my first it posting i have an error
Student.java:60: error: class, interface, or enum expected I am in java 101 so this is why it is such low level java, we only know the basics
import java.util.Scanner;
public class students
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many students?: ");
int numofstudents = keyboard.nextInt();
Student s = new Student();
s.setMultipleStudents();
s.toString();
System.out.println("Enter the Grade for the student: ");
int gradeofstudnets = keyboard.nextInt();
}
}
and my second class is
import java.util.Scanner;
public class Student
{
Scanner scan = new Scanner(System.in);
private String name;
private int grade;
private int[] multiplegradeinputs = new int[10];
private String[] multipleStudent = new String[10];
public Student()
{
}
public Student(String n, int g)
{
name = n;
grade = g;
}
public String setMultipleStudents()
{
String n = "";
for(int i = 1; i < multipleStudent.length; i++)
{
System.out.println("Enter student #" + i +" name: " );
n = scan.nextLine();
multipleStudent[i] = n;
}
return null;
}
public String multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
} <--- error here
public String toString()
{
String temp = "";
for(int i = 1; i < multipleStudent.length; i++)
{
temp += multipleStudent[i] + " ";
}
return temp;
}
}

Add return statement in your multiplegradeinputs() method:
public String multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
return null; //Add this line
}
Or change your methods to void return type if they dont return anything.

Class names have to be capitalized in java, so instead of
public class students
you should write
public class Students
Also instead of writing
keyboard.nextInt();
You should write
Integer.parseInt(keyboard.nextLine());
This is mainly because java is full of bugs and technical specifications that you won't find easily. Let me know if this fixes it for you, since you didn't post the exact error message you got.
As for the error that you pointed out, it's because your function expects a String as a return value no matter what, so either change that to void if you can or return a null string. To do that just add the following line at the very end of the method.
return null;

You should create a Student object which holds the properties of the student, e.g. Name and Grades. You should then store all the student objects in some kind of data structure such as an array list in the students class.

Adding to the answer provided by #hitz
You have a bug in the for loops:
for(int i = 1; i <multiplegradeinputs.length; i++)
for(int i = 1; i < multipleStudent.length; i++)
You will never populated multiplegradeinputs[0] and multipleStudent[0] because you start the loop at index == 1 and thus you will have only 9 student names stored instead of 10.
Change to:
for(int i = 0; i <multiplegradeinputs.length; i++)
for(int i = 0; i < multipleStudent.length; i++)
Remember even though the length in 10, the indices always start with 0 in Java and in your case will end with 9.

import java.util.Scanner;
public class Student
{
Scanner scan = new Scanner(System.in);
private String name;
private int grade;
private int[] multiplegradeinputs = new int[10];
private String[] multipleStudent = new String[10];
public Student()
{
}
public Student(String n, int g)
{
name = n;
grade = g;
}
public String setMultipleStudents()
{
String n = "";
for(int i = 1; i < multipleStudent.length; i++)
{
System.out.println("Enter student #" + i +" name: " );
n = scan.nextLine();
multipleStudent[i] = n;
}
return null;
}
public void multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
}
public String toString()
{
String temp = "";
for(int i = 1; i < multipleStudent.length; i++)
{
temp += multipleStudent[i] + " ";
}
return temp;
}
}
this is the 2nd class
import java.util.Scanner;
public class students
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many students?: ");
int numofstudents = keyboard.nextInt();
Student s = new Student();
s.setMultipleStudents();
s.toString();
System.out.println("Enter the Grade for the student: ");
int gradeofstudnets = keyboard.nextInt();
}
}

You are missing a return value in the multiplegradeinputs() method.

Related

Using a for loop to print out students and their grades from highest to lowest , using an object

So in this program I am asking the size of a class of students, taking in each student and the grade associated with them. myStudents[i] then holds each students name and their grade. The problem I am having now is with both of my selectionSort. I was supposed to arrange each students by grades (from highest to lowest. Which I think I did correctly in public static void selectionSort(student[] myStudents), but I am not sure how I a supposed to print that out using a for loop when I call selectionSort. Any advice to point me in the right direction would be greatly appreciated. Thank you!
import java.util.Scanner;
public class Final4{
public static void main(String[] args) {
Scanner myInput=new Scanner(System.in);
System.out.print("Enter the size of the class: ");
int num = myInput.nextInt();
int array[] = new int[num];
double score;
student[] myStudents = new student[num];
for (int i = 0 ; i < array.length; i++ ) {
System.out.print("Please enter a student name: ");
myInput.useDelimiter(System.getProperty("line.separator"));
String s;
s = myInput.next();
System.out.print("Please enter " + s +"'s score: ");
score = myInput.nextDouble();
myStudents[i] = new student(s, score);
}
}
selectionSort()
public static void selectionSort(student[] myStudents) {
for (int i = myStudents.length-1; i>0; i--){
int maxIndex = 0;
student max = myStudents[0];
for (int j=1; j<=i; j++)
if (myStudents[j].getScore() > (max.getScore())) {
maxIndex = i;
}
student temp = myStudents[i];
myStudents[i] = myStudents[maxIndex];
myStudents[maxIndex] = temp;
}
}
}
class student {
String name;
double score;
student(String name, double score) {
this.name = name;
this.score = score;
}
public double getScore() {
return score;
}
public String getName() {
return name;
}
}
I tend to get a little confused once I start incorporating gets and objects. Again, any advice is greatly appreciated.
so your problem was how to print your array of Student which is sorted by Selection Sort, right?
On your main method, after looping condition (on creating Student array) add this code.
selectionSort(myStudents);
for(Student s: myStudents) {
System.out.println(s.getName() + " " + s.getScore());
}
That code will print your array of students.

Java passing class array into array

I am a student and looking for help with an assignment. Here is the task: Create a CollegeCourse class. The class contains fields for the course ID (for example, “CIS 210”), credit hours (for example, 3), and a letter grade (for example, ‘A’).
Include get() and set()methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get() and set() method for the Student ID number. Also create a get() method that returns one of the Student’s CollegeCourses; the method takes an integer argument and returns the CollegeCourse in that position (0 through 4). Next, create a set() method that sets the value of one of the Student’s CollegeCourses; the method takes two arguments—a CollegeCourse and an integer representing the CollegeCourse’s position (0 through 4).
I am getting runtime errors on the second for loop where I am trying to get the data into the course array. It is asking for both the CourseID and Hours in the same line and regardless of what I respond with it I am getting an error, it almost seems like it is trying to get all the arrays variables at the same time. Here is my code which includes three classes. Any help to send me in the right direction is appreciated as I have spent a ton of time already researching to resolve.
public class CollegeCourse {
private String courseId;
private int creditHours;
private char grade;
public CollegeCourse(String id, int hours, char grade)
{
courseId=id;
creditHours = hours;
this.grade = grade;
}
public void setCourseId(String id)
{
courseId = id;//Assign course id to local variable
}
public String getCourseId()
{
return courseId;//Provide access to course id
}
public void setHours(int hours)
{
creditHours = hours;//Assign course id to local variable
}
public int getHours()
{
return creditHours;//Provide access to course id
}
public void setGrade(char grade)
{
this.grade = grade;//Assign course id to local variable
}
public char getGrade()
{
return grade;//Provide access to course id
}
}
Student Class
public class Student {
final int NUM_COURSES = 5;
private int studentId;
private CollegeCourse courseAdd;//Declares a course object
private CollegeCourse[] courses = new CollegeCourse[NUM_COURSES];
//constructor using user input
public Student(int studentId)
{
this.studentId=studentId;
}
public void setStudentId(int id)
{
studentId = id;//Assign course id to local variable
}
public int getStudentId()
{
return studentId;//Provide access to course id
}
public void setCourse(int index, CollegeCourse course)
{
courses[index] = course;
}
public CollegeCourse getCourse(int index)
{
return courses[index];
//do I need code to return the courseId hours, grade
}
}
InputGrades Class
import java.util.Scanner;
public class InputGrades {
public static void main(String[] args) {
final int NUM_STUDENTS = 2;
final int NUM_COURSES = 3;
Student[] students = new Student[NUM_STUDENTS];
int s;//subscript to display the students
int c;//subscript to display courses
int stId;
int csIndex;
String courseId = "";
int hours = 0;
//String gradeInput;
char grade = 'z';
CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly
Scanner input = new Scanner(System.in);
for(s = 0; s<NUM_STUDENTS; ++s)
{
students[s] = new Student(s);
System.out.print("Enter ID for student #" + (s+1) + ":");
stId = input.nextInt();
input.nextLine();
students[s].setStudentId(stId);
for(c=0; c < NUM_COURSES; ++c)
{
csIndex=c;
System.out.print("Enter course ID #" + (c+1) + ":");
courseId = input.nextLine();
course.setCourseId(courseId);
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
course.setGrade(grade);
students[s].setCourse(csIndex, course);
}
}
for(s = 0; s<NUM_STUDENTS; ++s)
{
System.out.print("\nStudent# " +
students[s].getStudentId());
System.out.println();
for(c=0;c<NUM_COURSES;++c)
System.out.print(students[s].getCourse(c) + " ");
System.out.println();
}
}
}
After input.nextInt() you need to add one more input.nextLine(); and than you can read grade.
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
Why it is needed? See this question: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
You should add a very simple length validation when you input the grade:
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
so the full main class code:
import java.util.Scanner;
/**
* Created by dkis on 2016.10.22..
*/
public class App {
public static void main(String[] args) {
final int NUM_STUDENTS = 10;
final int NUM_COURSES = 5;
Student[] students = new Student[NUM_STUDENTS];
//String name;
int s;//subscript to display the students
int c;//subscript to display courses
int stId;
int csIndex;
String courseId = "";
int hours = 0;
char grade = 'z';
CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly
Scanner input = new Scanner(System.in);
for(s = 0; s<NUM_STUDENTS; ++s)
{
students[s] = new Student(s);
System.out.print("Enter ID for student #" + s+1 + ":");
stId = input.nextInt();
input.nextLine();
students[s].setStudentId(stId);
for(c=0; c < NUM_COURSES; ++c)
{
//CollegeCourse course = students[s].getCourse(c);
csIndex=c;
System.out.print("Enter course ID#" + c+1 + ":");
courseId = input.nextLine();
course.setCourseId(courseId);
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
course.setGrade(grade);
students[s].setCourse(csIndex, course);
}
}
for(s = 0; s<NUM_STUDENTS; ++s)
{
System.out.print("\nStudent# " +
students[s].getStudentId());
for(c=0;c<NUM_COURSES;++c)
System.out.print(students[s].getCourse(c) + " ");
System.out.println();
}
}
}

ClassRoll and Constructor methods

public class Exams {
private int score1 = 0;
private int score2 = 0;
private int score3 = 0;
public void setScore1(int sc){
score1 = sc;
}
public void setScore2(int sc){
score2 = sc;
}
public void setScore3(int sc){
score3 = sc;
}
public int getScore1(){
return score1;
}
public int getScore2(){
return score2;
}
public int getScore3(){
return score3;
}
public String toString(){
return String.format("%-10s %-10s %4.2f\n", score1, score2, score3);
}
}
public class Student {
private String fName;
private String lName;
private Exams scores;
Student(String fn, String ln) {
fName = fn;
lName = ln;
scores = new scores();
}
public void setScore1(int sc) {
scores.setScore1(sc);
}
public void setScore2(int sc) {
scores.setScore2(sc);
}
public void setScore3(int sc) {
scores.setScore3(sc);
}
public String toSring(){
return String.format("%-10s %-10s %4.2f\n", fName, lName, scores);
}
public double getAverage(){
}
public int compareTo(Student s){
String name1 = lName + " " + fName;
String name2 = s.lName + " " + s.fName;
return ((lName + " " + fName).compareTo(s.lName + " " + s.fName));
}
}
public class ClassRoll {
private ArrayList<Student> students = new ArrayList<Student>();
private String title;
private String filename = "data.txt";
ClassRoll(String f) {
Scanner kb = new Scanner(System.in);
String inpFileName = kb.next();
File inpFile = new File(inpFileName);
Student s = new Student(fName, lName);
}
void Remove() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
students.remove(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void Display() {
}
void Add() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
System.out.println("What is the Student's second score?");
int score2 = kb.nextInt();
System.out.println("What is the Student's third score?");
int score3 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
System.out.println("Student already in class");
} else {
students.add(s);
}
}
}
void changeScore1() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore1(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void changeScore2() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore2(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void changeScore3() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore3(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
public void sortAverage() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = i + 1; j < students.size(); j++) {
Student s1 = (Student) students.get(i);
Student s2 = (Student) students.get(j);
if (s1.getAverage() < s2.getAverage()) {
students.set(i, s2);
students.set(j, s1);
}
}
}
}
public void sortNames() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = i + 1; j < students.size(); j++) {
Student s1 = (Student) students.get(i);
Student s2 = (Student) students.get(j);
if (s1.compareTo(s2) > 0) {
students.set(i, s2);
students.set(j, s1);
}
}
}
}
public void save(){
}
}
I have a program with a couple of different classes. In the classroll class after I declare the private variables I have to create a classroll constructor "ClassRoll(String f)" that is suppose to .....
Read the class roll data from the input file f, creates Student objects for each of the students and adds them to the ArrayList of students. The input file contains the course title on the first line. The data for each student appears on a separate line consisting of first name, last name, score1, score 2, and score3 separated by at least one space.
I tried my best to start it off but I'm confused and don't really know the right way of making it. Can someone please help
Thank you
Read the class roll data from the input file f, creates Student
objects for each of the students and adds them to the ArrayList of
students.
Ok, so basically, we have to get student's information from some file and add it to some ArrayList. Alright, lets create an ArrayList for storing stuff first.
ArrayList<Student> studentInfo = new ArrayList<Student>();
You already have this code to read from file:
Scanner kb = new Scanner(System.in);
String inpFileName = kb.next();
File inpFile = new File(inpFileName);
Lets move on....
The input file contains the course title on the first line.
So first element in kb.nextLine() is the course title. This means that we have to start adding student data from second line aka skip the first line. Alright....
The data for each student appears on a separate line consisting of
first name, last name, score1, score 2, and score3 separated by at
least one space.
So basically, if we manage to split lines from space characters, we get data. Now to implement this:
Lets create a boolean variable to check if its first line or not:
boolean firstLine = true; // True - because it starts from first line
Time to start reading lines....
String courseName = ""; //I'll just store in a string, use as you wish
while(kb.hasNextLine()){
if(firstLine){
courseName = kb.nextLine(); //Set courseName if its first line
firstLine = false; // We have moved past first line
}
else{ // Otherwise get student data
// Lets store the student data in a string. This is just one line.
// Something like: "FirstName LastName score1 score2 score3"
String studentData = kb.nextLine();
// Now time to split up data (so we get names/scores separately)
String[] stData = studentData.split("\\s"); //Remember they have spaces between them. \\s --> A pattern that matches one or more spaces
//So we can use those to separate data
// This is how String[] stData contains the student information:
// stData[0] = first name of student
// stData[1] = last name of student
// stData[2] = score1
// stData[3] = score2
// stData[4] = score3
// Use this data however you like
//Now that we have separated data, lets add the student object to ArrayList since we got all details.
studentInfo.add(new Student(stData[0], stData[1]);
// This last line of code is probably not going to do the trick.
// You may need to make changes to your code to do stuff
}
}
In any case, I have told you how to read the problem and how to proceed. You have all the strings, the firstname, lastname and scores. Now its upto you to figure out how to use them. Although the last line of code may not work for your, but its a hint to proceed.
Rest you should try figure out yourself first, since its no fun if I do all the homework ;)
You have replaced the SE with the scire1,2,3
When you get a var in setter method it need to be placed in a property of your class.
Just replace with this code:
public void setScore1(int sc){
score1=sc;
}
public void setScore2(int sc){
score2=sc;
}
public void setScore3(int sc){
score3=sc;
}

Java: The constructor gerbil(int) is undefined

So I've read over all of the constructor undefined posts on stackoverflow and tried the solutions and they haven't worked for me. Maybe I'm trying it wrong. I keep getting "the constructor Gerbil(int) is undefined."
The code that's the problem:
GerbilArray[i] = new Gerbil(i);
My full code:
import java.util.Scanner;
public class Gerbil {
public String name;
public String id;
public String bite;
public String escape;
public Gerbil() {
this.name = "";
this.id = "";
this.bite = "";
this.escape = "";
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many foods?");
int totalFood = keyboard.nextInt();
System.out.println("How many gerbils in the lab?");
int numberOfGerbils = keyboard.nextInt();
Gerbil[] GerbilArray = new Gerbil[numberOfGerbils];
for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil(i);
System.out.print("Lab ID:");
String id = keyboard.next();
System.out.print("Gerbil Nickname:");
String name = keyboard.next();
System.out.print("Bite?");
String bite = keyboard.next();
System.out.print("Escapes?");
String city = keyboard.nextLine();
for (int j = 0; j < totalFood; j++) {
System.out.println("How many of food " + (j+1) + "do you eat?:");
}
}
}
}
Also you've probably seen that my nested for-loop isn't finished as well. I'm trying to make an array inside of an object that will store "x" amount of integers inside of my object listed from the user (int totalFood) but I have no idea how.
You don't have a constructor Gerbil(int a) in the class Gerbil and you try to call it!
Just call it this way:
GerbilArray[i] = new Gerbil();

Print students in an array

I am trying to have the user type in the last name and first name of a student in an array so that I can call the student information and use it in a grade book.
The Student class has a method called Student(String last_name, String first_name)
I cannot figure out how to make it print students in a list such as:
last name, first name
last name, first name
Here is my program so far:
public static void main (String[] args)
{
System.out.println("--------------------------------");
System.out.println("Welcome to the Gradebook Program");
System.out.println("--------------------------------");
System.out.println();
students = GetNumberOfStudents();
//Allocate space for student information
Student student[] = new Student[students];
for (int i = 0; i < students; i++)
{
System.out.println("Student #" + (i+1));
System.out.print("\tEnter last name: ");
student[i] = scan.nextLine();
System.out.print("\tEnter first name: ");
student[i] = scan.nextLine();
}
System.out.println(student[i]);
I expect we would need to see the definition of Student, you have given the constructor but not the getters/setters.
I would expect the printing code to look something like
for (Student s : student) {
System.out.println(s.getLastName() + "," + s.getFirstName());
}
You are also not initialising your Student objects correctly.
Inside the loop you have written I would expect to see
new Student(lastname, firstname);
Here is a soltuion with a student class which looks like in your description.
package test; //change to your package name
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student{
private String m_LastName;
private String m_FirstName;
private int[] TestScores = new int[3];
//Constructor
public Student(String last, String first) {
m_LastName = last;
m_FirstName = first;
}
//returns firstname
public String firstName() {
return m_FirstName;
}
//returns lastname
public String lastName() {
return m_LastName;
}
//set a test score at a given index
public void setTestScore(int index, int score) {
TestScores[index] = score;
}
//returns test score at a given index
public int testScore(int index) {
return TestScores[index];
}
//returns testscores average
public double testAverage() {
int sum = 0;
for(int i = 0; i<TestScores.length; i++) {
sum += TestScores[i];
}
return sum/TestScores.length;
}
//returns students highest test score
public int maxTestScore() {
//sort the array
for(int i = 0; i<TestScores.length; i++) {
for(int j = 0; j<TestScores.length; j++) {
if(TestScores[i]<TestScores[j]) {
int buffer;
buffer = TestScores[i];
TestScores[i] = TestScores[j];
TestScores[j] = buffer;
}
}
}
return TestScores[TestScores.length-1];
}
public boolean isPassing() {
//TODO: when hes passing?
return true;
}
public static void main (String[] args)
{
Scanner scan = new Scanner(new InputStreamReader(System.in));
System.out.println("--------------------------------");
System.out.println("Welcome to the Gradebook Program");
System.out.println("--------------------------------");
System.out.println();
/**
* dont know where you declare the students variable
* and how the getnumberofstudents function looks like
*
* students = GetNumberOfStudents();
*/
int students = 1;
List<Student> StudentList = new ArrayList<>(); //creat a list which can store student objects
for (int i = 0; i < students; i++)
{
System.out.println("Student #" + (i+1));
System.out.print("\tEnter last name: ");
String lastname = scan.nextLine(); //store lastname in a variable
System.out.print("\tEnter first name: ");
String firstname = scan.nextLine(); //store firstname in a variable
Student student = new Student(lastname, firstname); //creat new student object with the last and firstname
StudentList.add(student); //add it to the student list
}
//print out all students first and lastnames. here you can add the other data you want to print.
for (int i = 0; i < students; i++)
{
System.out.println("List of all Students:");
System.out.println("Firstname:"+StudentList.get(i).firstName()+" Lastname:"+StudentList.get(i).lastName());
}
}
}

Categories

Resources