0

This is my program in which i am trying to generate menu driven program

program


import java.util.*;

public class LinkedListDemo {
            
    LinkedList<String> list = new LinkedList<String>();
        
            static class Node
            {
                int data=0;
                Node next=null;
                
                Node(int data)
                {
                    this.data=data;
                }
                Node()
                {

                }
                Node(int data, Node next)
                {
                    this.data=data;
                    this.next=next;
                }

            }
                
                
               public void addGrp() {
                     
                
                try (Scanner sc = new Scanner(System.in)) 
                {
                    System.out.println("Enter Group lead Name");
                    list.add(sc.nextLine());
                    
                    // Use add() method to add
                    // elements into the LinkedList
                    
                    System.out.println("Enter Student Name");
                    list.add(sc.nextLine());
                    
                    System.out.println("Enter Student Name");
                    list.add(sc.nextLine());
                    System.out.println("Enter Student Name");
                    list.add(sc.nextLine());
                    System.out.println("Enter Student Name");
                    list.add(sc.nextLine());
                    System.out.println("Enter Student Name");
                    list.add(sc.nextLine());
                    System.out.println("Enter Student Name");
                    list.add(sc.nextLine());            
                    
                // Displaying the LinkedList
                    System.out.println("The LinkedList: "
                                + list);
                    Scanner scn=new Scanner(System.in);
                    System.out.println("enter the name of your project group");
                    String groupname=scn.nextLine();    
                    System.out.print("Project group names is : "+groupname+ " and Members are ");
                    for(String elements:list)
                    System.out.print(elements+" ");
                // Creating the array and using toArray()
                    String[] arr = new String[7];
                    list.toArray(arr);
                }catch(Exception e) {
                    e.printStackTrace();
                }
                }
                //Print all elements of the Array
               public void display() {
                    
                    
                    
                
                    }
               public static void main(String arg[])
                {
                    LinkedListDemo ll=new LinkedListDemo();
                // Creating an empty LinkedListLinearLinkedList obj= new LinearLinkedList();
                    Scanner sc=new Scanner(System.in);
                    int ch;
                    
                    do {
                        System.out.println("\n1.addGrp\n2.display\n0.Exit");
                        ch=sc.nextInt();
                        switch(ch)
                        {
                        case 1:
                            ll.addGrp();
                            break;
                        case 2:
                            ll.display();
                            break;
                        }
                        
                    }while(ch!=0);
                        

                }
                
                }

error:


1.addGrp
2.display
0.Exit
1
Enter Group lead Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
The LinkedList: [a, a, a, a, a, a, a]
enter the name of your project group
a
Project group names is : a and Members are a a a a a a a 
1.addGrp
2.display
0.Exit
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at LinkedListDemo.main(LinkedListDemo.java:87)

error:


1.addGrp
2.display
0.Exit
1
Enter Group lead Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
Enter Student Name
a
The LinkedList: [a, a, a, a, a, a, a]
enter the name of your project group
a
Project group names is : a and Members are a a a a a a a 
1.addGrp
2.display
0.Exit
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at LinkedListDemo.main(LinkedListDemo.java:87)

program is running but not giving me desired output and has this errors i have created two methods one is display method and another is inserting elements and i want to insert student data into array and want store that array as an object into linkedlist so tell me how to solve this error

1 Answers1

0

This is a tricky one, since you are initializing and closing the Scanner in two separate places (implicitly). See a good explanation in this answer [here] (java.util.NoSuchElementException - Scanner reading user input) (Check the accepted answer).

A brief excerpt from the answer below:

... in first method, it (closing the scanner) not only closes your scanner but closes your System.in input stream as well.

One way to handle this is to initialize the scanner in the main method and pass it as a parameter to the other methods. See the code below:

import java.util.*;

public class LinkedListDemo {

LinkedList<String> list = new LinkedList<String>();

static class Node {
    int data = 0;
    Node next = null;

    Node(int data) {
        this.data = data;
    }

    Node() {

    }

    Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }

}


public void addGrp(Scanner sc) {

    sc.nextLine(); // NOTE: Added to 'consume' the new line character left over by sc.nextInt()
    try {
        System.out.println("Enter Group lead Name");
        list.add(sc.nextLine());

        // Use add() method to add
        // elements into the LinkedList

        System.out.println("Enter Student Name");
        list.add(sc.nextLine());

        System.out.println("Enter Student Name");
        list.add(sc.nextLine());
        System.out.println("Enter Student Name");
        list.add(sc.nextLine());
        System.out.println("Enter Student Name");
        list.add(sc.nextLine());
        System.out.println("Enter Student Name");
        list.add(sc.nextLine());
        System.out.println("Enter Student Name");
        list.add(sc.nextLine());

        // Displaying the LinkedList
        System.out.println("The LinkedList: "
                + list);
        Scanner scn = new Scanner(System.in);
        System.out.println("enter the name of your project group");
        String groupname = scn.nextLine();
        System.out.print("Project group names is : " + groupname + " and Members are ");
        for (String elements : list)
            System.out.print(elements + " ");
        // Creating the array and using toArray()
        String[] arr = new String[7];
        list.toArray(arr);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//Print all elements of the Array
public void display() {


}

public static void main(String arg[]) {
    LinkedListDemo ll = new LinkedListDemo();
    // Creating an empty LinkedListLinearLinkedList obj= new LinearLinkedList();

    int ch;
    Scanner sc = new Scanner(System.in);

    do {
        System.out.println("\n1.addGrp\n2.display\n0.Exit");
        ch = sc.nextInt();
        switch (ch) {
            case 1:
                ll.addGrp(sc);
                break;
            case 2:
                ll.display();
                break;
        }

    } while (ch != 0);

    sc.close();

}

}

Also note the use of sc.nextLine() in the addGrp method - it is added to consume the extraneous new-line character left over by nextInt(). (There are other ways to handle this - see here)

Hope this helps

Vini
  • 8,299
  • 11
  • 37
  • 49