简体   繁体   English

如何将扫描仪输入转换为 integer?

[英]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.

相关问题 如何使用ResultSet中的Scanner user_input Integer变量在Java中选择特定的MySQL行? - How do I select specific MySQL row in Java using Scanner user_input Integer variable in ResultSet? Java - 如何在不使用 while 循环的情况下检查 Scanner 输入是否为整数? - Java - How do I check if a Scanner input is an integer without using a while loop? 扫描仪:如何限制n个字符后的扫描仪输入? - Scanner: How do I limit the Scanner input after n characters? 如何使用Java Scanner接受整数输入? NoSuchElementException - How can I use Java Scanner to take an integer input? NoSuchElementException 我如何获得一个整数来设置与扫描仪相同的值 - How do i get an integer to set the value the same as a scanner 如何将 BufferedReader-Input (String) 转换为 Integer 并将其保存在 java 中的整数列表中? - How do I convert a BufferedReader-Input (String) to Integer and save it in an Integer List in java? 我想将扫描仪的整数输入限制在一个范围内 - I want to limit the integer input for the scanner in a range 如何从扫描仪获取多个整数输入并将每个整数存储在单独的数组中? - How can I take multiple integer input from scanner and store each integer in separate arrays? 如何使用Scanner检查用户输入是否为整数? - How to check the user input is an integer or not with Scanner? 如何从 Java Scanner 获取整数输入 - How to get Integer input from Java Scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM