public static void main(String[] args) { List courses = getCourseList(); List students= getStudents(); ...But I need both lists to be joined together so that I can use a System.out.println to display both lists together/at the same time. Is there a way to do that? Here are my java files: testStudent.java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class testStudent { public static void main(String[] args) { List courses = getCourseList(); List students= getStudents(); for(Student st:students) { System.out.println(st); } } public static List getStudents() { List returnList = new ArrayList(); BufferedReader reader; try { reader = new BufferedReader(new FileReader("ClassesData.dat")); String line = reader.readLine(); while (line != null) { Student tempStudent = new Student(); tempStudent.setCourseId(Integer.parseInt(line)); line = reader.readLine(); tempStudent.setClassName(line); line = reader.readLine(); tempStudent.setCredits(Integer.parseInt(line)); line = reader.readLine(); tempStudent.setStudentName(line); line = reader.readLine(); tempStudent.setMajor(line); line = reader.readLine(); tempStudent.setGrade(line); line =reader.readLine(); returnList.add(tempStudent); if(tempStudent.getGrade().equals("A")) { tempStudent.setComment("Great job!"); } else { tempStudent.setComment("-"); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } return returnList; } public static List getCourseList() { List returnList = new ArrayList(); BufferedReader reader; try { reader = new BufferedReader(new FileReader("CoursesData.dat")); String line = reader.readLine(); while (line != null) { Course tempCourse = new Course(); tempCourse.setCourseId(Integer.parseInt(line)); line = reader.readLine(); tempCourse.setInstructorId(line); line = reader.readLine(); tempCourse.setRoomId(line); line = reader.readLine(); returnList.add(tempCourse); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return returnList; } } Student.java public class Student { private String studentName; private int courseId; private String major; private String className; private String roomId; private String instructorId; private String grade; private int credits; private String comment; public Student() { super(); } public Student(String studentName, int courseId, String major, String className, String roomId, String instructorId, String grade, int credits, String comment) { super(); this.studentName = studentName; this.courseId = courseId; this.major = major; this.className = className; this.roomId = roomId; this.instructorId = instructorId; this.grade = grade; this.credits = credits; this.comment = comment; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentName() { return studentName; } public void setCourseId(int courseId) { this.courseId = courseId; } public int getCourseId() { return courseId; } public void setMajor(String major) { this.major = major; } public String getMajor() { return major; } public void setClassName(String className) { this.className = className; } public String getClassName() { return className; } public void setRoomId(String roomId) { this.roomId = roomId; } public String getRoomId() { return roomId; } public void setInstructorId(String instructorId) { this.instructorId = instructorId; } public String getInstructorId() { return instructorId; } public void setGrade(String grade) { this.grade = grade; } public String getGrade() { return grade; } public void setCredits(int credits) { this.credits = credits; } public int getCredits() { return credits; } public void setComment(String comment) { this.comment = comment; } public String getComment() { return comment; } public String toString() { return "Student Name: " + studentName + " || Course ID: " + courseId + " || Class Name: " + className + " || Room ID: " + roomId + " || Instructor ID: " + instructorId + " || Grade: " + grade + " || Credits: " + credits + " || Comments: " + comment + ""; } } Course.java public class Course { private int courseId; private String instructorId; private String roomId; public Course() { super(); } public Course(int courseId, String instructorId, String roomId) { super(); this.courseId = courseId; this.instructorId = instructorId; this.roomId = roomId; } public int getCourseId() { return courseId; } public void setCourseId(int courseId) { this.courseId = courseId; } public String getInstructorId() { return instructorId; } public void setInstructorId(String instructorId) { this.instructorId = instructorId; } public String getRoomId() { return roomId; } public void setRoomId(String roomId) { this.roomId = roomId; } public String toString() { return "Course ID: " + courseId + " || Instructor ID: " + instructorId + " || Room ID: " + roomId + ""; } }
Hello. I am trying to figure out a way to print out two lists at the same time. I have my lists written as such:
public static void main(String[] args)
{
List<Course> courses = getCourseList();
List<Student> students= getStudents();
...But I need both lists to be joined together so that I can use a System.out.println to display both lists together/at the same time. Is there a way to do that?
Here are my java files:
testStudent.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class testStudent
{
public static void main(String[] args)
{
List<Course> courses = getCourseList();
List<Student> students= getStudents();
for(Student st:students)
{
System.out.println(st);
}
}
public static List<Student> getStudents()
{
List<Student> returnList = new ArrayList<Student>();
BufferedReader reader;
try
{
reader = new BufferedReader(new FileReader("ClassesData.dat"));
String line = reader.readLine();
while (line != null)
{
Student tempStudent = new Student();
tempStudent.setCourseId(Integer.parseInt(line));
line = reader.readLine();
tempStudent.setClassName(line);
line = reader.readLine();
tempStudent.setCredits(Integer.parseInt(line));
line = reader.readLine();
tempStudent.setStudentName(line);
line = reader.readLine();
tempStudent.setMajor(line);
line = reader.readLine();
tempStudent.setGrade(line);
line =reader.readLine();
returnList.add(tempStudent);
if(tempStudent.getGrade().equals("A"))
{
tempStudent.setComment("Great job!");
}
else
{
tempStudent.setComment("-");
}
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return returnList;
}
public static List<Course> getCourseList()
{
List<Course> returnList = new ArrayList<Course>();
BufferedReader reader;
try
{
reader = new BufferedReader(new FileReader("CoursesData.dat"));
String line = reader.readLine();
while (line != null)
{
Course tempCourse = new Course();
tempCourse.setCourseId(Integer.parseInt(line));
line = reader.readLine();
tempCourse.setInstructorId(line);
line = reader.readLine();
tempCourse.setRoomId(line);
line = reader.readLine();
returnList.add(tempCourse);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return returnList;
}
}
Student.java
public class Student
{
private String studentName;
private int courseId;
private String major;
private String className;
private String roomId;
private String instructorId;
private String grade;
private int credits;
private String comment;
public Student()
{
super();
}
public Student(String studentName, int courseId, String major, String className,
String roomId, String instructorId, String grade, int credits, String comment)
{
super();
this.studentName = studentName;
this.courseId = courseId;
this.major = major;
this.className = className;
this.roomId = roomId;
this.instructorId = instructorId;
this.grade = grade;
this.credits = credits;
this.comment = comment;
}
public void setStudentName(String studentName)
{
this.studentName = studentName;
}
public String getStudentName()
{
return studentName;
}
public void setCourseId(int courseId)
{
this.courseId = courseId;
}
public int getCourseId()
{
return courseId;
}
public void setMajor(String major)
{
this.major = major;
}
public String getMajor()
{
return major;
}
public void setClassName(String className)
{
this.className = className;
}
public String getClassName()
{
return className;
}
public void setRoomId(String roomId)
{
this.roomId = roomId;
}
public String getRoomId()
{
return roomId;
}
public void setInstructorId(String instructorId)
{
this.instructorId = instructorId;
}
public String getInstructorId()
{
return instructorId;
}
public void setGrade(String grade)
{
this.grade = grade;
}
public String getGrade()
{
return grade;
}
public void setCredits(int credits)
{
this.credits = credits;
}
public int getCredits()
{
return credits;
}
public void setComment(String comment)
{
this.comment = comment;
}
public String getComment()
{
return comment;
}
public String toString()
{
return "Student Name: " + studentName + " || Course ID: " + courseId + " || Class Name: " + className
+ " || Room ID: " + roomId + " || Instructor ID: " + instructorId + " || Grade: " + grade + " || Credits: " + credits
+ " || Comments: " + comment + "";
}
}
Course.java
public class Course
{
private int courseId;
private String instructorId;
private String roomId;
public Course()
{
super();
}
public Course(int courseId, String instructorId, String roomId)
{
super();
this.courseId = courseId;
this.instructorId = instructorId;
this.roomId = roomId;
}
public int getCourseId()
{
return courseId;
}
public void setCourseId(int courseId)
{
this.courseId = courseId;
}
public String getInstructorId()
{
return instructorId;
}
public void setInstructorId(String instructorId)
{
this.instructorId = instructorId;
}
public String getRoomId()
{
return roomId;
}
public void setRoomId(String roomId)
{
this.roomId = roomId;
}
public String toString() {
return "Course ID: " + courseId + " || Instructor ID: " + instructorId + " || Room ID: " + roomId + "";
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps