0

I am a beginner in java and I am trying to do this simple project, which is an app that adds new student to the school system, I am having a problem in a method "courses", this methods have to return a String with the courses the user picked (first they are stored in a list, then with the help of Arrays.toString they are converted to a string. so how you can sees from the code the user inter the number of the courses and that number saved in variable n then a list being created with that length, then inside the loop the student choose what courses. the program works half through then shows errors :

public String courses() {
            int k;
            Scanner inpot = new Scanner(System.in);
            System.out.println("How maney courses do u want to enroll in ?");
            int n = inpot.nextInt();
            String[] courses = new String[n];
            System.out.println("choose the courses");
            System.out.println("\n 1 : Math \n 2 : History \n 3 : computer scince \n 4: lecture");
            
            for(int i = 0; i < n ; i++) {
                 k = inpot.nextInt();
                if(k == 1) {
                    courses[i] = "math";
                }
                else if(k == 2) {
                    courses[i] = "History";
                }
                else if( k == 3) {
                    courses[i] = "Computer scince";
                }
                else if (k == 4) {
                    courses[i] = "lecture";
                }
                
                System.out.println("your couse number " + (i + 1) + " has been saved");
                
                if((i+1 == n)) {
                    System.out.println("you enterd all the courses");
                    
                    
                }
                else {
                System.out.println("enter your next cours");
                }

            }
            inpot.close(); 
            
            return Arrays.toString(courses);
        }
     
How maney courses do u want to enroll in ?
2
choose the courses

 1 : Math 
 2 : History 
 3 : computer scince 
 4: lecture
1
your couse number 1 has been saved
enter your next cours
2
your couse number 2 has been saved
you enterd all the courses
How maney courses do u want to enroll in ?
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Studentdatabase.Student.courses(Student.java:36)
    at Studentdatabase.Studentdatabase.main(Studentdatabase.java:13)

Here is also the other class that have main in it

public class Studentdatabase {

    public static void main(String[] args) {
        
        
        
        Student s1 = new Student("Lucas", "Moller", 8); 
        
        String m = s1.courses();
        System.out.println(m);

    }
rem208
  • 79
  • 4
  • `inpot.close();` looks wrong - why are you closing the standard input stream? – UnholySheep Sep 16 '20 at 08:00
  • 2
    inpot.close() is the problem. You get the error you listed when you read from a Scanner that has been closed. You won't be able to open the scanner again, because closing the scanner also closes System.in. Please do not close scanners on System.in. Just reuse them. – NomadMaker Sep 16 '20 at 08:07

0 Answers0