I am a bit new to Java and coding in general and I came across a problem that so far I can't solve. The functionality is: A switch menu which asks for input to be saved on an array (option 1) and then with the second option the attributes of the objects in the array be printed.
I have a custom class:
Course
public class Course {
String name_course;
String code_course;
int credits_course;
Course(String name, String code, int credits){
this.name_course = name;
this.code_course = code;
this.credits_course = credits;
}
}
In another file I have defined a function for the input of the user be saved on the array and also the function to loop over the array and print the values.
public class Logic{
static BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
static PrintStream out = System.out;
//I believe this function does not save correctly the input on the array
static Course[] course = new Course[6];
public static void register_course(String name, String code, int credits) {
for (int i = 0; i < course.length; i++) {
course[i] = new Course(name, code, credits);
}
// This only prints one value as the previous function is likely wrong
public static void print_course(Course[] pcourse) {
for (int i = 0; i < course.length; i++) {
out.println(course[i]);
}
}
}
Here is the switch I have on the main
// Just to clarify I have a do while loop that loops over the switch but I won't include it, it works fine
public static void process_option(int pOption) throws IOException{
switch(pOption){
case 1:
out.println("Name");
String name = in.readLine();
out.println("Code");
String code = in.readLine();
out.println("Credits");
int credits = Integer.parseInt(in.readLine());
Logic.register_course(name, code, credits);
break;
case 2:
Logic.print_course(Logic.course);
break;
}
I would really appreciate any help to figure out my error. Thanks.