Array of objects problem

[size=1][/size]Hi,
I am new to here and new to Java, so please take it easy on me! lol
I am trying to create an array of objects with the initial dataset coming from a txt file. Here is a sample of the txt file ..

COSC1073:Programming 1
ISYS1057:Database Concepts
COSC1082:Computer Organization
MATH1074:Mathematics for Computing

Here is my code to extract the data and add into an array of Course objects...
[code]public class gradCheck {

public static void main(String[] args) throws IOException {
//Create Array for the objects
Course[] courseArray = new Course[18];

//Create an instance of the file
java.io.File courses = new java.io.File("courses.txt");

//Create the scanner
Scanner input = new Scanner(courses);

//Read data from the file
while(input.hasNext()){
for (int i = 0; i < courseArray.length; i++){
input.useDelimiter(":");
String courseCode = input.next();
String courseName = input.nextLine();
courseArray[i] = new Course (courseCode, courseName);

System.out.println(courseArray[i]);
}
}
input.close();

//below is my Course class

public class Course {

String courseCode;
String courseName;

Course (){
courseCode = "null";
courseName = "null";
}

Course (String courseCode, String courseName){
getCourseCode(courseCode);
getCourseName(courseName);
}

public String getCourseCode(String courseCode){
return setCourseCode(courseCode);
}

private String setCourseCode(String courseCode) {
this.courseCode = courseCode;
return courseCode;
}

public String getCourseName(String courseName){
return setCourseName(courseName);
}

private String setCourseName(String courseName) {
this.courseName = courseName;
return courseName;
}
}[/code]

Sample of the output when printing contents in the object array.
Course@69b332
Course@173a10f
Course@530daa
Course@a62fc3 ...

Should be more like ...
COSC1073:Programming 1
ISYS1057:Database Concepts
COSC1082:Computer Organization ...

Comments

  • When you're printing out your data it should be

    System.out.println(courseArray[i].getCourseCode() + ":" + courseArray[i]getCourseName());
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion