Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m getting an error in my code that i cannot figure out what it is. Heres is t

ID: 3687480 • Letter: I

Question

I'm getting an error in my code that i cannot figure out what it is. Heres is the code i've made.

import java.io.*;
import java.util.*;

public class dLinkDemo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
  
DLinkList list = new DLinkList();
char chr;
boolean continueInput = true;
  
do
{
System.out.println(" Sorted double link list");

System.out.println("a. Add");
System.out.println("d. Delete");
System.out.println("p. Print forward");
System.out.println("r. Print backward");
System.out.println("q. Quit");
  
  
String choice = scan.nextLine().toUpperCase();
char ch = choice.charAt(0);
  
  
switch(ch)
{
case 'A':
System.out.println("Enter integer element to insert");
list.insert(scan.nextInt());
break;
  
case 'D' :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize())
System.out.println("Invalid position ");
else
list.delete(p);
break;
  
case 'P' :
System.out.println(" Display in ascending order = ");
list.printForward();
break;

case 'R' :
System.out.println(" Display in reverse order = ");
list.printBackward();
break;
  
case 'Q' :
System.exit(1);
continueInput = false;
break;
  
default :
System.out.println("Wrong Entry ");
break;
}
  
  

}//do   
while (continueInput);
  
  
  
  
  

}
}

here is my error:

**first run is fine**

Sorted double link list
a. Add
d. Delete
p. Print forward
r. Print backward
q. Quit

** i input 'a' from the menu**

a

**The programs asks for integer**

Enter integer element to insert
12

**Menu repeats and an error is given**

Sorted double link list

a. Add
d. Delete
p. Print forward
r. Print backward
q. Quit
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
   at java.lang.String.charAt(String.java:646)
   at dLinkDemo.main(dLinkDemo.java:26)

Explanation / Answer

Hi, I have updated the code. Its working fine now. Below two lines

String choice = scan.nextLine().toUpperCase();
char ch = choice.charAt(0);
  

Replaced By

String choice = scan.next().toUpperCase();
char ch = choice.charAt(0);

import java.io.*;

import java.util.*;
public class dLinkDemo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
  
DLinkList list = new DLinkList();
char chr;
boolean continueInput = true;
  
do
{
System.out.println(" Sorted double link list");

System.out.println("a. Add");
System.out.println("d. Delete");
System.out.println("p. Print forward");
System.out.println("r. Print backward");
System.out.println("q. Quit");
  
  String choice = scan.next().toUpperCase();
char ch = choice.charAt(0);

  
switch(ch)
{
case 'A':
System.out.println("Enter integer element to insert");
list.insert(scan.nextInt());
break;
  
case 'D' :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize())
System.out.println("Invalid position ");
else
list.delete(p);
break;
  
case 'P' :
System.out.println(" Display in ascending order = ");
list.printForward();
break;

case 'R' :
System.out.println(" Display in reverse order = ");
list.printBackward();
break;
  
case 'Q' :
System.exit(1);
continueInput = false;
break;
  
default :
System.out.println("Wrong Entry ");
break;
}
  
  

}//do   
while (continueInput);
  
  
  
  
  

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote