Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got both classes of my code. Skip down to about 3/4 just above the main method to public class Roster. The errors seem to be in reference to my ArrayList not liking type Students(String didn't work either). Can someone put this in an IDE and help me figure out what's wrong?

I'm getting errors on the lines that read:
1)Students newStudent = new String(studentId, firstName, lastName, email, age, grades);
2)studentRoster.get(i).print();
3)int average = (newStudent.getGrades()[0] + newStudent.getGrades()[1]+ newStudent.getGrades()[2])/3;
4) System.out.println("Student Number: " + newStudent.getstudentId() + " average grade: " + average);
5)if(b.getstudentId().equals(studentId))
6)if(b.getQuantity() > 0)

What I have tried:

Java
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

public class Student{
	//public static void main(String[] args){}
		//make instance variables
		private String studentId;
		private String firstName;
		private String lastName;
		private String email;
		private String age;
		private int[] grades;
				
		//do constructors
	public Student(String studentId, String firstName, String lastName, String email, String age, int[] grades){
		setStudentId(studentId);
		setFirstName(firstName);
		setLastName(lastName);
		setEmail(email);
		setAge(age);
		setGrades(grades);
	}
		
		//accessors and mutators
		public String getStudentId(){  /*why do you hate me? */
			return studentId;
		}

		public void setStudentId(String studentId){
			this.studentId = studentId;
		}
		
		public String getFirstName(){
			return firstName;
		}
		public void setFirstName(String firstName){     /*holy crap, go away errors*/
			this.firstName = firstName;
		}
		
		public String getLastName(){
			return lastName;
		}
		
		public void setLastName(String lastName){
			this.lastName = lastName;
		}
		
		public String getEmail(){
			return email;
		}
		
		public void setEmail(String email){
			this.email = email;
		}
		
		public String getAge(){
			return age;
		}
		
		public void setAge(String age){
			this.age = age;
		}
		
		public void setGrades(int[] grades){
			this.grades = grades;
		}
		public int[] getGrades(){
			return grades;

import java.util.ArrayList;
public class Roster {
	private static ArrayList<students> studentRoster = new ArrayList<>();
	
	public static void main(String[] args){
		//add all 5 students
			add("1","John", "Smith","John1989@gmail.com","20", 88, 79, 59);
			add("2","Suzan","Erickson","Erickson_1990@gmail.com","19", 91, 72, 85);
			add("3","Jack","Napoli","The_lawyer99@yahoo.com","19", 85, 84, 87);
			add("4","Erin","Black","Erin.black@comcast.net","22", 91, 98, 82);
			add("5","Jason","Hancock","jhanc21@wgu.edu","30", 98, 95, 96);
			printAllStudents();
			printAverageGrades();
	}
		public static void add(String studentId, String firstName, String lastName, String email, String age, int grades1, int grades2, int grades3){
			int[] grades = {grades1, grades2, grades3};
			Students newStudent = new String(studentId, firstName, lastName, email, age, grades); //why doesn't this work?
			studentRoster.add(newStudent);
		}
		public static void printAllStudents(){
			System.out.println("Student Roster");
			for(int i=0; i < studentRoster.size(); i++){
				studentRoster.get(i).print();  //why doens't this work?
		}
	}
		public static void printAverageGrades(){
			System.out.println("Average Grades");
			for(Students newStudent : studentRoster){
				int average = (newStudent.getGrades()[0] + newStudent.getGrades()[1]+ newStudent.getGrades()[2])/3;
				System.out.println("Student Number: " + newStudent.getstudentId() + " average grade: " + average);
			}
		}

		public static void findStudent(String studentId){
			for(Students b: studentRoster){
				if(b.getstudentId().equals(studentId)){
					if(b.getQuantity() > 0){
						System.out.println(studentId + " is a student.");
						return;
					}else{
						System.out.println(studentId + " is not a student.");
						return;
					}
				}
			}
			System.out.println("We do not have a student number " + studentId);
		}
					
	}

Eclipse is accepting my private static ArrayList<students> studentRoster = new ArrayList<>;

I know Students isn't a type, but I tried String, and that didn't work either.
Posted
Updated 19-Feb-17 21:47pm
v2
Comments
[no name] 19-Feb-17 21:06pm    
"Can someone put this in an IDE and help me figure out what's wrong?", debugging your code is your job. It's a useful skill that you must develop.
Member 13010927 19-Feb-17 21:32pm    
I know what's wrong. I just have no idea how to fix it. I've spent the last three days on google trying to figure this out. Its a project that I have to do for my college and there's no one to call over the weekend. Its not due any time in the near future. I'm just tired of beating my head on the keyboard. I figured if someone could put it in an IDE, they could see what's wrong more easily
[no name] 19-Feb-17 21:39pm    
Sorry.... and? It's not our job to debug your code for you. It doesn't take all that much effort for you to copy/paste whatever the errors are you see on your screen. Why should we put more effort into fixing your homework assignment for you than you put into it?
wseng 19-Feb-17 21:46pm    
Your class are Student, but you declare Students in your class Roster

This is the case of one mistake leads to another. I will just answer to your title:
You have a class
public class Student{
, then you try to
private static ArrayList<students> studentRoster = new ArrayList<>();
Do you notice the mismatch in the names here. Shouldn't it be
ArrayList<Student> studentRoster = new ArrayList<Student>();
You have to figure out the rest yourself.
 
Share this answer
 
Comments
Member 13010927 19-Feb-17 23:42pm    
I tried that earlier, it didn't work. Of course, I'm going to assume that needs to be done and it didn't work because of something else I don't know about. Thanks for your reponse
1. Your class are Student but you define Students in your class Roster.

2. Is getStudentId() not getstudentId() in Roster.

3. Cannot find symbol getQuantity() in Roster because you are not declaring them in class Student

4. Why don't you use
System.out.println(studentRoster.get(i)); 
instead of
studentRoster.get(i).print();
 
Share this answer
 
v4
Comments
Member 13010927 20-Feb-17 0:01am    
How classes read from other classes still doesn't make too much sense to me. I feel like I should've known better on 4. Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900