java输入scanner
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.
该代码编译没有错误,并且似乎不会引起任何问题。 如果只接受任何一个类中的输入,即调用obj1.input()或obj2.input() ,则程序将完美运行。 但是,在我们调用两个类的输入函数的情况下,如图所示,我们在运行时得到以下输出。
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.
我们看到第一个输入[ obj1.input() ]可以正常工作,但是在第二个类中进行输入时却出错。 如果我们交换调用这两个函数的顺序,我们将能够在第二类而不是第一类中接受输入。 我们观察到,在多个类中使用Scanner对象时,总是会出错。 这是因为Scanner类的第一个对象在输入流上进行了锁定。 即使我们使用Scanner类的close()方法,由于流被Scanner类的第一个对象锁定,我们仍然会收到错误。
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
翻译自: https://www.includehelp.com/java/exceptions-with-scanner-class-and-solution-in-java.aspx
java输入scanner