类型在验证用户输入为整数时无法转换

huangapple go评论73阅读模式
英文:

types cannot be converted when validating the users input for an integer

问题

import java.util.Scanner;

public class Sums {

    public static int number;

    public static void main(String[] args) {
        int evenSum, oddSum = 0;
        int posInt;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a positive integer: ");
        int number = input.nextInt();
        try {
            Integer.parseInt(number);
            System.out.println("Value entered is " + number);
        } catch (NumberFormatException e) {
            System.out.println(number + " is not an integer.");
        }
    }
}
英文:

I am trying to validate a user's input and check if the inputted number is an integer. Id like to throw them an error message if the number isn't an integer. I am getting stuck on the line Integer.parseInt(number); Any suggestions would be very appreciated.

import java.util.Scanner;

public class Sums {

    public static int number;

    public static void main(String[] args) {
        int evenSum, oddSum = 0;
        int posInt;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a postitive integer: ");
        int number = input.nextInt();
        try {
            Integer.parseInt(number);
            System.out.println("Value entered is " + number);
        } catch (NumberFormatException e) {
            System.out.println(number + " is not an integer.");
        }
    }
}

答案1

得分: 0

你不需要显式地将你的数字转换为整数,Scanner.nextInt() 已经为您执行了这个操作,即 Scanner.nextInt() 会将输入的下一个标记扫描为一个 int 值。所以只需从您的代码中删除这一行:"int number = input.nextInt();"。一般情况下,当您想将表示整数值的 String 转换为带符号的十进制整数时,最好使用 Integer.parseInt()。例如,如果您使用的是 Scanner.nextLine() 而不是 Scanner.nextInt(),那么如果您想对该整数值执行后续操作,将其转换为带符号的十进制整数会更有意义。

英文:

You don't need explicitly to convert your number to an integer, Scanner.nextInt() already does that for you, i.e. Scanner.nextInt() scans the next token of the input as an int. So just remove the line "int number = input.nextInt();" from your code. In general, Integer.parseInt() is preferable when you want to convert a String representation of an Integer value as a signed decimal integer. For instance, if you would have used Scanner.nextLine() instead of Scanner.nextInt(), then it makes more sense to convert it to a signed decimal integer if you want to perform subsequent operations using that integer value.

答案2

得分: 0

问题在于当你调用 Integer.parseInt(number) 时,你试图解析变量 number,而该变量是一个 int 而不是一个 String。这意味着你在进行错误检查时是从 int 转换为 Integer,而不是从 String 转换为 Integer

要解决这个问题,输入一个字符串而不是一个数字,然后在字符串上执行转换,而不是在数字上执行转换。

...

public static void main(String[] args) {
    int myNumber;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a positive integer: ");
    String numberString = input.nextLine();
    try {
        myNumber = Integer.parseInt(numberString);
        System.out.println("Value entered is " + myNumber);

        // 进行你想要的操作

    } catch (NumberFormatException e) {
        System.out.println(numberString + " is not an integer.");
    }
}

...
英文:

The issue here is that when you call Integer.parseInt(number) you are trying to parse the variable number which is an int and not a String. This means that you are doing the error checking on a conversion from an int to an Integer and not a String to an Integer.

To fix this, input a string instead of a number, then perform the conversion on the string, not the number.

...

public static void main(String[] args) {
    int myNumber;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a postitive integer: ");
    String numberString = input.nextLine();
    try {
        myNumber = Integer.parseInt(number);
        System.out.println("Value entered is " + myNumber);

        // do what you want with the number

    } catch (NumberFormatException e) {
        System.out.println(numberString + " is not an integer.");
    }
}

...

答案3

得分: 0

我已将 int number = input.nextInt(); 修改为 String number = input.next();,并且它能够正常工作。

英文:

I have changed the int number = input.nextInt(); to String number = input.next(); and it works.

答案4

得分: 0

int number = input.nextInt();

变量 <b>number</b> 已经是一个 int这就是为什么你不能将它传递给该方法该方法需要一个字符串

如果你想像你之前做的那样使用该方法你应该使用
```java
input.next()

作为参数,因为它会将输入作为字符串返回,并在 try 块内初始化 number,例如:

      public static void main(String[] args) {
        int evenSum, oddSum = 0;
        int posInt;
        System.out.println("输入一个正整数:");
        Scanner input = new Scanner(System.in);
        try {
            number = Integer.parseInt(input.next());
            System.out.println("输入的值是 " + number);
        } catch (NumberFormatException e) {
            System.out.println("不是整数。");
        }
    }
英文:
int number = input.nextInt();

The variable <b>number</b> is already an int, that is why you can not hand it over to the method. The method requires a String.

If you want to use the method like you did, you should use

input.next()

as argument because it returns the input as String and initialize number within the try block, e.g.

      public static void main(String[] args) {
        int evenSum, oddSum = 0;
        int posInt;
        System.out.println(&quot;Enter a postitive integer: &quot;);
        Scanner input = new Scanner(System.in);
        try {
            number = Integer.parseInt(input.next());
            System.out.println(&quot;Value entered is &quot; + number);
        } catch (NumberFormatException e) {
            System.out.println(&quot;Not an integer.&quot;);
        }
    }

答案5

得分: 0

Bug here is int number = input.nextInt();<br>

当你想要将一个 String 转换为一个基本数据类型时,我们使用 class.parseXXX(String)<br>
你可以看到我在函数 .parseXXX(String) 中写了 String,因为它接受 String 作为参数,没有其他内容<br>
所以让我们来看看你的代码

Integer.parseInt(number);

这里是 .parseXXX(int),很明显它与 .parseXXX(String) 不匹配<br>
所以为了以 String 作为参数,你需要从用户那里接受一个 String 输入<br>

将这个 int number = input.nextInt(); 替换为 String number = input.next();

所以现在下面的代码是有效的,因为它是在 .parseXXX(String)

Integer.parseInt(number);

修改后的代码:

        int evenSum, oddSum = 0;
        int posInt;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a positive integer: ");
        String number = input.next(); // &lt;-- 已更改
        try {
            Integer.parseInt(number);
            System.out.println("Value entered is " + number);
        } catch (NumberFormatException e) {
            System.out.println(number + " 不是一个整数。");
        }
英文:

Bug here is int number = input.nextInt();<br>

When you want to convert a String to a primitive datatype we use class.parseXXX(String)<br>
You see I have written String in the function .parseXXX(String) Cause it takes String as a parameter and nothing else<br>
So lets read your code

Integer.parseInt(number);

Here its .parseXXX(int) and clearly its not matching .parseXXX(String)<br>
So in order to give String as parameter, you have accept String as input from user.<br>

Replace this int number = input.nextInt(); with String number = input.next();

So now below code is valid cause its in .parseXXX(String)

Integer.parseInt(number);

Changed code:

        int evenSum, oddSum = 0;
        int posInt;
        Scanner input = new Scanner(System.in);
        System.out.println(&quot;Enter a postitive integer: &quot;);
        String number = input.next(); // &lt;-- Changed
        try {
            Integer.parseInt(number);
            System.out.println(&quot;Value entered is &quot; + number);
        } catch (NumberFormatException e) {
            System.out.println(number + &quot; is not an integer.&quot;);
        }

huangapple
  • 本文由 发表于 2020年10月12日 01:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定