3

The following Java method is suppose to accept a string (name of a person) from the user via keyboard, search that name in an array called name[] and delete that person's name from the array (by assigning name[i] = "INVALID").

The code tries to accept the input string (the name of the person) using a Scanner class object del_name, but I am getting a NoSuchElementException in the statement

s=del_name.next();

i.e the 4th statement from the top.

I will be really grateful if someone can give a solution and also explain as to why this code is not working. (Thank You)

void Deletee()
{


      Scanner del_name=new Scanner(System.in);

      String s;

      System.out.println("Enter the name to be deleted");

      s=del_name.next(); // causing NoSuchElementException

      int i=0;

   /* find position in which the name occurs using while-loop below */


      while(!s.equalsIgnoreCase(name[i]) && i<count) 

             i++ ;  // increment i to search in next array index



      if(i<count)
      {
          name[i]="INVALID";

          count--;

          System.out.println("Deletion Successful");
      }
      else
      {

          System.out.println("No such person exist");

      }

      del_name.close();


}
user1803551
  • 12,965
  • 5
  • 47
  • 74
plz_ans
  • 31
  • 1

3 Answers3

1

Change .next() to .nextLine().

Scanner del_name=new Scanner(System.in);

String s;

System.out.println("Enter the name to be deleted");

s=del_name.nextLine(); 

Scanner.next() returns whatever the current input is, even if there is none (giving you your error). Scanner.nextLine() skips past the current line, and returns the skipped portion.

Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
0

Try with,

del_name.nextLine()

Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.

next(): Finds and returns the next complete token from this scanner.
nextLine(): Advances this scanner past the current line and returns the input that was skipped.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

The code that you posted does not throw an exception as-is. You might have done something prior to this method call that caused this. As proof, run this code:

public class Test {

    static String[] name = new String[] {"AAA", "BBB", "CCC"};

    static void deletee() {

        Scanner delName = new Scanner(System.in);
        System.out.println("Enter the name to be deleted");
        String s = delName.next();

        boolean found = false;
        for (int i = 0; i < name.length; i++) {
            if (name[i].equalsIgnoreCase(s)) {
                name[i] = "INVALID";
                System.out.println("Deletion Successful");
                found = true;
                break;
            }
        }
        if (!found)
            System.out.println("No such person exist");
        delName.close();
    }

    public static void main(String[] args) {

        deletee();
        for (int i = 0; i < name.length; i++)
            System.out.print(name[i] + ", ");
    }
}

Notes:

  • Method names should start with lower case.
  • Non-final variables should not use underscores, just uppercases where a new word begins.
  • I rewrote the searching mechanism for something more intuitive, although there are many more ways to do this.
  • Good job on closing the scanner.
user1803551
  • 12,965
  • 5
  • 47
  • 74