Home » Java programming language

Exception with Scanner class and solution while taking input in Java

Here, we are going to learn how to solve a common error encountered while trying to take input in various classes (using Scanner Class) in a Java program?
Submitted by Saranjay Kumar, on March 18, 2020

Consider the following code,

import java.util.*;

public class test {
    public static void main(String args[]) {
      c1 obj1 = new c1();
      obj1.input();
      c2 obj2 = new c2();
      obj2.input();
    }
}

class c1
{
    int age;
    String name;
    
    void input()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter age");
        age = in.nextInt();
        System.out.println("Enter name");
        name = in.next();
        System.out.println("Name: " + name + " Age: "+ age);
    }
}

class c2
{
    float perc;
    String id;
    
    void input()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter percentage");
        perc = in.nextFloat();
        System.out.println("Enter ID");
        id = in.next();
        System.out.println("Id: "+ id + " Percentage: "+ perc);
        
    }
}

The code compiles without errors and does not seem to cause any problem. If we take input in any one class ONLY, that is, either call obj1.input() OR obj2.input(), the program works perfectly. However, in the case where we call the input function of both the classes, as shown, we get the following output at run time.

Output

Enter age
18
Enter name
abc
Name: abc Age: 18
Enter percentage

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.nextFloat(Scanner.java:2496)
	at c2.input(test.java:37)
	at test.main(test.java:8)

We see that the first input [ obj1.input() ] works fine but we get an error while taking input in second class. If we exchange the sequence in which the two functions are called, we will be able to take input in second class but not in first. We observe that we always get an error while using Scanner object in multiple classes. This is because the first object of Scanner class makes a lock on the input stream. Even if we use the close() method of Scanner class, we still get the error because the stream gets locked by the first object of Scanner class.

The solution to the problem is to dedicate a separate class to handle all input operations. Consider the following update to our program.

import java.util.*;

public class test {
    public static void main(String args[]) {
      c1 obj1 = new c1();
      obj1.input();
      c2 obj2 = new c2();
      obj2.input();
    }
}

class c1
{
    int age;
    String name;
    
    void input()
    {
        System.out.println("Enter age");
        age = inputclass.in.nextInt();
        System.out.println("Enter name");
        name = inputclass.in.next();
        System.out.println("Name: " + name + " Age: "+ age);
    }
}

class c2
{
    float perc;
    String id;
    
    void input()
    {
        System.out.println("Enter percentage");
        perc = inputclass.in.nextFloat();
        System.out.println("Enter ID");
        id = inputclass.in.next();
        System.out.println("Id: "+ id + " Percentage: "+ perc);
        
    }
}
class inputclass
{
    static Scanner in = new Scanner(System.in);
}

Output

Enter age
18
Enter name
abc
Name: abc Age: 18
Enter percentage
89.1
Enter ID
AD101
Id: AD101 Percentage: 89.1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.