[英]How do I convert scanner input into an integer?
In Java, how, if possible, can I convert numerical scanner input (such as 2 or 87) into an integer variable?在 Java 中,如果可能的话,如何将数字扫描仪输入(例如 2 或 87)转换为 integer 变量? What I'm using now yields the error message:
我现在使用的是什么错误信息:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at diDecryption.Didecryption.main(Didecryption.java:226)
And this is the code I'm using to do it (pieced together, it's part of a much larger program):这是我用来执行此操作的代码(拼凑在一起,它是一个更大程序的一部分):
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
String name=sc.next();
int result = Integer.valueOf(name);
if (result / 2 == 1){
System.out.println("a");
The purpose of the program is to decode an encrypted message.该程序的目的是解码加密的消息。 The input is numerical, and if I remove the string to int converter, the division does not work.
输入是数字,如果我删除字符串到 int 转换器,除法不起作用。 How do I fix this?
我该如何解决?
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
String name=sc.next();
int result = Integer.parseInt(name);
if (result / 2 == 1){
System.out.println("a");
parseint changes it to a primitive int rather than an Integer object parseint 将其更改为原始 int 而不是 Integer 对象
In Java, how, if possible, can I convert numerical scanner input (such as 2 or 87) into an integer variable?在Java中,如何将数字扫描仪输入(例如2或87)转换为整数变量? What I'm using now yields the error message:
我现在使用的是产生错误消息:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at diDecryption.Didecryption.main(Didecryption.java:226)
And this is the code I'm using to do it (pieced together, it's part of a much larger program):这是我用来执行此操作的代码(组合在一起,它是一个更大的程序的一部分):
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
String name=sc.next();
int result = Integer.valueOf(name);
if (result / 2 == 1){
System.out.println("a");
The purpose of the program is to decode an encrypted message.该程序的目的是对加密的消息进行解码。 The input is numerical, and if I remove the string to int converter, the division does not work.
输入是数字,如果我将字符串删除到int转换器,则该除法将不起作用。 How do I fix this?
我该如何解决?
In your stacktrace you have null
as parameter in Integer.valueOf(name)
.在您的堆栈跟踪中,您将
null
作为Integer.valueOf(name)
参数。 Seems your console produce some invalid input sequence.似乎您的控制台产生了一些无效的输入序列。 Try to check it with
sc.hasNext()
condition:尝试使用
sc.hasNext()
条件检查它:
System.out.println("Enter first number");
Scanner sc = new Scanner(System.in);
if (sc.hasNext()) {
String name = sc.next();
int result = Integer.parseInt(name);
if (result / 2 == 1) {
System.out.println("a");
}
}
Try尝试
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
int name=sc.nextInt();
if ((name / 2) == 1)
System.out.println("a");
RUN跑
run:
Enter first number
2
a
Try this code试试这个代码
package exmaple;
import java.util.Scanner;
public class Parser {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.next();
try{
int result = Integer.parseInt(name);
if(result / 2 == 1) {
System.out.println("a");
}
} catch(Exception exception) {
}
in.close();
}
}
try尝试
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
int result = sc.nextInt;
if (result / 2 == 1){
System.out.println("a");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.