[英]I get this error message Exception in thread “main” java.util.InputMismatchException
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(a);
}
}
I get the following message and don't know why.我收到以下消息,不知道为什么。
Exception in thread "main" java.util.InputMismatchException: For input string: "999999999999999"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at codingexercises.Main.main(Main.java:9)
I want to know how to fix the problem.我想知道如何解决这个问题。
999,999,999,999,999
is too large to fit in an int
. 999,999,999,999,999
太大而无法放入int
。
Use nextLong()
or nextBigInteger()
if you need to support numbers larger than 2,147,483,647
.如果您需要支持大于
2,147,483,647
的数字,请使用nextLong()
或nextBigInteger()
。
long
can handle values up to 9,223,372,036,854,775,807
. long
可以处理高达9,223,372,036,854,775,807
的值。
BigInteger
doesn't have a specified limit. BigInteger
没有指定的限制。
Replace代替
int a = sc.nextInt();
with和
long a = sc.nextLong()
In Java, the Java Language Specification determines the representation of the data types.在 Java 中, Java 语言规范确定了数据类型的表示。
The order is: byte 8 bits, short 16 bits, int 32 bits, long 64 bits.顺序为:byte 8位,short 16位,int 32位,long 64位。 All of these types are signed, there are no unsigned versions.
所有这些类型都是签名的,没有未签名的版本。 However, bit manipulations treat the numbers as they were unsigned (that is, handling all bits correctly).
但是,位操作将数字视为无符号数(即正确处理所有位)。
The character data type char is 16 bits wide, unsigned, and holds characters using UTF-16 encoding (however, it is possible to assign a char an arbitrary unsigned 16 bit integer that represents an invalid character codepoint)字符数据类型 char 为 16 位宽,无符号,并使用 UTF-16 编码保存字符(但是,可以为 char 分配任意无符号 16 位 integer,表示无效字符代码点)
width minimum maximum
SIGNED
byte: 8 bit -128 +127
short: 16 bit -32 768 +32 767
int: 32 bit -2 147 483 648 +2 147 483 647
long: 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
UNSIGNED
char 16 bit 0 +65 535
Try with the following code.尝试使用以下代码。 It will help to solve your problem:
这将有助于解决您的问题:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger a = sc.nextBigInteger();
System.out.println(a);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.