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
- Insert at beginning
- Insert at end
- Insert at any
- 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;
}
}
}