简体   繁体   中英

after calling a method the rest of code are not working in java

I tried to call a method(1) in another method(2). But after calling 1 method in 2 method the rest of the code lines not working in 2 method. Solution please.

first method

public static void student(){
    Scanner input = new Scanner(System.in);
    String[] student_id=new String[150];        
    System.out.print("Enter Student ID   : ");
    for(int i =0;i<student_id.length;i++){      
    student_id[i] = input.nextLine();

    String[] student_name=new String[150];      
    System.out.print("Enter Student Name : ");
    for(int j =0;j<student_name.length;j++){
    student_name[j] = input.nextLine();
    
    }
}}

second method

 public static void addNewStudent(){
    Scanner input = new Scanner(System.in);
    System.out.println("-------------------------------------------------------------------------------------");
    System.out.println("|                                ADD NEW STUDENT                                     |");
    System.out.print("-------------------------------------------------------------------------------------\n");
    System.out.println();
    
    student(); //calling 1 method.rest of codes are not working.

    System.out.println();   
    System.out.print("Student has been added successsfully. Do you want to add a new student (y/n): ");
    String yn = input.nextLine();
    
    if (yn.equals("y")){ 
        clearConsole();
        addNewStudent();
        } else {
            clearConsole();
            homePage();
        }
    }

The outer loop runs 150 times and reads a line, the inner loop runs 150 * 150 times and reads a line. That totals to 150 + 150 * 150 = 22650 lines to be read.

So the question is why a student would have 150 IDs and 22500 names.

There is definitely a flaw in the logic, also since addNewStudent() goes recursively on new students and does not even return when no more students are needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM