0

Doubly linked list A runtime error is occuring it skips entering name and ask to enter marks of student It does not let me to enter student name whenever tried to show error/ output like

  1. Insert at beginning
    1. Insert at end
    2. Insert at any
    3. printlist 1 Enter Student Name : Enter marks : Suraj

Exception in thread "main" java.lang.NumberFormatException: For input string: "Suraj" at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)

at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122) at java.base/java.lang.Float.parseFloat(Float.java:455) at student_1.main(student_1.java:32)

Process finished with exit code 1

Program

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class node
{
    String name;
    float marks;
    node prev;
    node next;

    public node(String name, float marks) {
        this.name = name;
        this.marks = marks;
    }
}

    public class student_1
    {
        node head;
        node last;
        public static void main(String[]args) throws IOException {
            student_1 s=new student_1();
            System.out.print("1. Insert at beginning\r\n 2. Insert at end\r\n 3. Insert at any\r\n 4. printlist\r\n");
         String name;float marks;
             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            char ch=(char)br.read();
            switch(ch) {
                case '1':
                    System.out.print("Enter Student Name : ");
                     name=br.readLine();
                    System.out.print("Enter marks : ");
                     marks=Float.parseFloat(br.readLine());
                s.InsBeg(name,marks);
                break;
                case '2':
                    System.out.print("Enter Student name: ");
                    name=br.readLine();br.readLine();
                    System.out.print("Enter marks : ");
                    marks=Float.parseFloat(br.readLine());
                    s.InsEnd(name,marks);
                case '3':
                    System.out.print("Enter Student name: ");
                    name=br.readLine();
                    System.out.print("Enter marks : ");
                    marks=Float.parseFloat(br.readLine());
                    s.InsEnd(name,marks);
                case '4':
                s.printlist(s.head);
            }

        }

        void InsBeg(String name, float marks)throws IOException {
            node n = new node(name, marks);
                if (head == null) {
                    head = n;
                    return;
                }

                n.prev = null;
                n.next = head;
                if (head != null) {
                    head.prev = n;

                }

                head = n;

            }


        void InsEnd(String name, float marks)throws IOException
        {
            node n=new node(name,marks);
            if(head==null)
            {head=n;
                return;
            }
           node last1=head;
            n.next=null;
            while(last1.next!=null) {
                last1 = last1.next;
            }
            last1.next=n;
            n.prev=last1;
            last=n;


        }
        public void InsAny(String name, float marks)throws IOException
        { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            node n=null;
            System.out.println("Position: ");
            int pos=Integer.parseInt(br.readLine());
            node prev_Node=head.prev;
            if (prev_Node == null) {
                System.out.println("The given previous node cannot be NULL ");
                return;
            }

             if(pos==1){
                 InsBeg(name, marks);return;}

                for (int i = 1; prev_Node.next!=null; ++i) {
                    if(i==pos) {
                        break;
                    }
                    prev_Node = prev_Node.next;
                }
            n.name=name;
            n.marks=marks;
            n.next = prev_Node.next;
            prev_Node.next = n;


            n.prev = prev_Node;
            if (n.next != null)
                n.next.prev = n;

        }
        public void printlist(node node)
        {
            node last = null;
            System.out.println("Traversal in forward Direction");
            System.out.print("Name "+ "\t\t\t\t\t\t"+"Marks"+"\n");
            while (node != null) {
                System.out.print(node.name + "\t\t\t\t"+node.marks+"\n");
                last = node;
                node = node.next;
            }
            System.out.println();
            System.out.println("Traversal in reverse direction");
            System.out.print("Name "+ "\t\t\t\t\t\t"+"Marks"+"\n");
            while (last != null) {
                System.out.print(last.name + "\t\t\t\t"+last.marks+"\n");
                last = last.prev;
            }
        }


    }
  • `NumberFormatException: For input string: "Suraj"` should be clear: you are trying to convert your name to a float – bb1950328 Jun 08 '20 at 10:02
  • It is not allowing me to enter string value –  Jun 08 '20 at 10:04
  • I got the issue, posting the answer in a minute wait – Zain Arshad Jun 08 '20 at 10:05
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Amongalen Jun 08 '20 at 10:06
  • @ Amongalen its not with scanner actually it is with BufferedReader –  Jun 08 '20 at 10:10

1 Answers1

0

So you are facing the problem of scanner reading \n, when you press enter after choosing the option from menu. This post will give you a in-depth understanding of the problem

Add this line:

br.readLine();

after char ch=(char)br.read(); this one.

Explanation: You are using br.read() and casting it into a char. When you enter "1", what your program gets is a 1\n. Note the trailing newline character, 1 is got consumed by the read() but \n is still "hanging there". When you want to read the name like this name=br.readLine(); program reads the \n and move on.

This is the reason it is not letting you enter anything, because it read \n.

Just a side note, you may want to add a while-loop so that your program just not simply ends after first try. It should prompt user again and again until user decides to end(you can add an option to quit)

Zain Arshad
  • 1,759
  • 1
  • 8
  • 20