-2

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.

Zan
  • 1
  • 1
  • if you are just starting out, you can always use lists, more specific arraylists. They are going to make your life *a lot* easier. [Overview here](https://www.callicoder.com/java-arraylist/) –  Sep 30 '19 at 20:28
  • I believe you're not having a code problem but a logic problem (though some might consider them the same thing). Take a step back and re-think through what you have to do, write it out in words even as a step by step process, then convert that outline to code. – xtratic Sep 30 '19 at 20:28
  • Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Jordan Sep 30 '19 at 20:28
  • The parameter for the `print` method is called `pcourse` but you end up printing the static one `course` parameter - I believe you need to either rename the parameter (to `course`) or print the array passed as parameter - named `pcourse` (instead of the staticly defined) - ie. change it to: `out.println(pcourse[i]);` – blurfus Sep 30 '19 at 20:30

1 Answers1

0

Welcome to stack overflow, I think you made your code a bit more complicated than needed the class Logic is not really needed. But less start from the class Course here is my version

public class Course {
    private String nameCourse;
    private String codeCourse;
    private int creditsCourse = 0;


   public void nameCourse(String name){
     this.nameCourse = name;
   }

   public void codeCourse(String name){
     this.codeCourse = name.toUpperCase();
   }


   public void creditsCourse(int credits){
     this.creditsCourse = credits;
   }


   @Override
   public String toString(){
       return "Course " + this.nameCourse + " with code " +  this.codeCourse + " credit for the course " + this.creditsCourse;
   }

}

it does exactly the same work of your but it uses setter instead of the constructor method have a look at this Setter. I also used the @Override annotation to override the standard toString() method of the Object class. Now to your implementation of the control the main problem with your Logic class is that you are limited to just 6 courses what if you need more Luckily for us, Java has ArrayList that can handle a dynamically sized array of any class Here is my example code

  public static void main(String[] args) {
    ArrayList<Course> courses = new ArrayList<>();
    Course course;
    for (int counter = 0; counter < 10; counter++) {
        course = new Course();
        course.codeCourse("cs" + counter);
        course.nameCourse("Computer Science " + counter);
        course.creditsCourse(10 + counter);
        courses.add(course);
    }

    for (Course c : courses) {
        System.out.println(c.toString());
    }

}

you can see the declaration ArrayList<Course> courses = new ArrayList<>() and the add to the list of the new course courses.add(course) and finally System.out.println(c.toString()) to print the content of the object as string.

The output looks like

Course Computer Science 0 with code CS0 credit for the course 10
Course Computer Science 1 with code CS1 credit for the course 11
Course Computer Science 2 with code CS2 credit for the course 12
Course Computer Science 3 with code CS3 credit for the course 13
Course Computer Science 4 with code CS4 credit for the course 14
Course Computer Science 5 with code CS5 credit for the course 15
Course Computer Science 6 with code CS6 credit for the course 16
Course Computer Science 7 with code CS7 credit for the course 17
Course Computer Science 8 with code CS8 credit for the course 18
Course Computer Science 9 with code CS9 credit for the course 19

I think you get the idea hopefully this has been useful have fun with Java

Balint
  • 295
  • 2
  • 11