2

I'm super confuse because how is it possible to get ArrayList from another method, and print it to another method? So my problem here is, my code is about student registration system where it have 2 methods which are admin and student, so from admin method, we can add arrays we want and when we print that out in admin method, it works fine. But when I try to print that out in student method, seems to be not working. Here's the code:

import java.util.*;

public class myStudent {

boolean logins = false;
int myId;
String addCourse="No Record";
String NewCourse;
String removeCourse="No Record";
int modifyCourse;
String password = null;
ArrayList test = new ArrayList();

public static void main(String[] args){

    myStudent obj0 = new myStudent();

    obj0.login();

}

void login(){
    myStudent obj1 = new myStudent();
    Scanner myScanner = new Scanner(System.in);

    System.out.println("  **Student Registration**");
    System.out.println("\t--LOGIN--");

    System.out.println("\n1)Login as student \n2)Login as admin \n3)Exit");
    System.out.print(">");
    int optionMenu = myScanner.nextInt();

    if(optionMenu == 1){
        obj1.showMenuStudent();
    }
    else if(optionMenu == 2){
        obj1.showMenuAdmin();
    }
    else if(optionMenu == 3){
        System.out.println("THANK YOU!");
        System.exit(0);
    }
 }

 void showMenuAdmin(){


        if (!logins) {
    //String myPass;
    Scanner myScanner = new Scanner(System.in);

    System.out.println("\n--LOGIN--");       
    try{
    System.out.print("Admin ID : ");
    myId = myScanner.nextInt();
    }
    catch(Exception e){
        System.out.println("INVALID ID!");
        System.exit(0);
    }

    System.out.print("Admin Password : ");
    password = myScanner.next();

        logins = true;
    }


    char option;
    Scanner myScanner = new Scanner(System.in);


    System.out.println("\n--WELCOME--");
    System.out.println("A. Create Course");
    System.out.println("B. Drop Course");
    System.out.println("C. Modify Course");
    System.out.println("D. Display Course");
    System.out.println("E. Logout");

    do
    {
    System.out.print("Enter An Option > ");
    option = myScanner.next().charAt(0);
    System.out.print("\n");


    switch(option){
        case 'A':

            System.out.print("Create Course(Maximum is 6) :");
            addCourse = myScanner.next();
            test.add(addCourse);
            System.out.println("\nCourse Added Successfully!");
            display();

        break;

        case 'B':


            Object[] myArray = test.toArray();

            System.out.println("Current Courses List: ");
            int k=1;
            for(int i=0; i < myArray.length; i++){

            System.out.println(k + ". " + myArray[i]);
            k++;
            }

                System.out.println("Enter course name you want to remove :");
                System.out.println("Enter 'back' to cancel");
                System.out.print(">");
                removeCourse = myScanner.next();
                if(removeCourse!="back"){
                test.remove(removeCourse);
                System.out.println("\nSucceed");
                display();
                }else
                display();

            break;

        case 'C':
            Object[] myArray2 = test.toArray();

            System.out.println("Current Courses List: ");
            int m=1;
            for(int i=0; i < myArray2.length; i++){

            System.out.println(m + ". " + myArray2[i]);
            m++;
            }

            System.out.println("Select course you want to modify :");

            System.out.print(">");
            modifyCourse = myScanner.nextInt();

            try{
            if(modifyCourse==1){
                System.out.print("Change to :");
                NewCourse=myScanner.next();
                test.set(0, NewCourse);
                System.out.println("Succeed!");
                display();
            }
            else if(modifyCourse==2){
                System.out.print("Change to :");
                NewCourse=myScanner.next();
                test.set(1, NewCourse);
                System.out.println("Succeed!");
                display();
            }else if(modifyCourse==3){
                System.out.print("Change to :");
                NewCourse=myScanner.next();
                test.set(2, NewCourse);
                System.out.println("Succeed!");
                display();
            }else if(modifyCourse==4){
                System.out.print("Change to :");
                NewCourse=myScanner.next();
                test.set(3, NewCourse);
                System.out.println("Succeed!");
                display();
            }else if(modifyCourse==5){
                System.out.print("Change to :");
                NewCourse=myScanner.next();
                test.set(4, NewCourse);
                System.out.println("Succeed!");
                display();
            }else if(modifyCourse==6){
                System.out.print("Change to :");
                NewCourse=myScanner.next();
                test.set(5, NewCourse);
                System.out.println("Succeed!");
                display();
            }
            else{
                System.out.print("Invalid!");
                display();
            }
            }
            catch(Exception e){
                System.out.print("Invalid, Course not available!");
                display();
            }

            break;


        case 'D':

            Object[] myArray3 = test.toArray();

            System.out.println("Courses List: ");
            int j=1;
            for(int i=0; i < myArray3.length; i++){

            System.out.println(j + ". " + myArray3[i]);
            j++;
            }
            display();
        break;
        }

    }while(option != 'E');

   System.out.println("Logged Out Successfully.Thank You!");
   login();


 }

 void showMenuStudent(){

    if (!logins) {

    Scanner myScanner = new Scanner(System.in);

    System.out.println("\n--LOGIN--");

    try{
    System.out.print("Student ID : ");
    myId = myScanner.nextInt();
    }
    catch(Exception e){
        System.out.println("INVALID ID!");
        System.exit(0);
    }

    System.out.print("Student Password(Default is IC Number) : ");
    password = myScanner.next();

        logins = true;
    }

    Object[] myArray4 = test.toArray();
    int n=1;
    for (Object myArray41 : myArray4) {
        System.out.println(n + ". " + myArray41);
        n++;
    }


 }

 void display(){
    showMenuAdmin();
 }
 }

I really hope someone would understand my problem and trying to fix it.. Please :( and thank you

Here's the output from beginning to the result:

OUTPUT:

Student Registration

--LOGIN--

1)Login as student

2)Login as admin

3)Exit

2

--LOGIN--

Admin ID : 123

Admin Password : 321

--WELCOME--

A. Create Course

B. Drop Course

C. Modify Course

D. Display Course

E. Logout

Enter An Option > A

Create Course(Maximum is 6) :CS123

Course Added Successfully!

--WELCOME--

A. Create Course

B. Drop Course

C. Modify Course

D. Display Course

E. Logout

Enter An Option > E

Logged Out Successfully.Thank You!

Student Registration

--LOGIN--

1)Login as student

2)Login as admin

3)Exit

1

--LOGIN--

Student ID : 123

Student Password(Default is IC Number) : 321

[]Enter An Option >

//at this point, idk why it run like that,I just want to print out what the admin had entered in the array,but its like the student method just rerun the admin method again even i never call it, and it also shown that the array is empty. Which part is my mistake? or am i missing something here? :(

5
  • Can you also paste the output Commented May 5, 2020 at 10:54
  • yeah there's the output Commented May 5, 2020 at 11:06
  • You have multiple instances of myStudent. Each instance has its own List. Thus, the List being acted upon in the student method is not the same List you acted on in the admin method. Commented May 5, 2020 at 11:25
  • You are creating new object myStudent obj1 = new myStudent(); in login method so it will create new list for you Commented May 5, 2020 at 12:22
  • yeah thanks man! appreciate it :D Commented May 5, 2020 at 12:25

1 Answer 1

1

So i ran your code and found the problem. You're creating a new myStudent object at every login(), If you:

  • Remove the line myStudent obj1 = new myStudent();
  • And replace obj1.showMenuStudent(); with showMenuStudent()
  • And replace obj1.showMenuAdmin(); with showMenuAdmin();

You will have fixed this issue and will get the output you want. Here is the output I got, hopefully its in sync with what you want to obtain:

  **Student Registration**
    --LOGIN--

1)Login as student 
2)Login as admin 
3)Exit
>2

--LOGIN--
Admin ID : 1
Admin Password : 1

--WELCOME--
A. Create Course
B. Drop Course
C. Modify Course
D. Display Course
E. Logout
Enter An Option > A

Create Course(Maximum is 6) :CS123

Course Added Successfully!

--WELCOME--
A. Create Course
B. Drop Course
C. Modify Course
D. Display Course
E. Logout
Enter An Option > E

Logged Out Successfully.Thank You!
  **Student Registration**
    --LOGIN--

1)Login as student 
2)Login as admin 
3)Exit
>1
1. CS123
Enter An Option > 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.