Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在将int转换为arr。在线程中不断获取错误异常;“主要”;java.util.InputMismatchException_Java_Arrays_Eclipse_Int - Fatal编程技术网

正在将int转换为arr。在线程中不断获取错误异常;“主要”;java.util.InputMismatchException

正在将int转换为arr。在线程中不断获取错误异常;“主要”;java.util.InputMismatchException,java,arrays,eclipse,int,Java,Arrays,Eclipse,Int,我正在写一个程序来测试信用证号码的有效性。这就是我到目前为止所写的,第一个检查正确数量的数字的if语句有效,如果没有足够的数字,则输出无效。但是,第二个if语句不起作用,我不确定原因。每当我输入一个同时满足if语句的数字时,我都会收到错误消息: **线程“main”java.util.InputMismatchException中的异常:对于输入字符串:“4444” 位于java.util.Scanner.nextInt(未知源) 位于java.util.Scanner.nextInt(未知源)

我正在写一个程序来测试信用证号码的有效性。这就是我到目前为止所写的,第一个检查正确数量的数字的
if
语句有效,如果没有足够的数字,则输出无效。但是,第二个
if
语句不起作用,我不确定原因。每当我输入一个同时满足
if
语句的数字时,我都会收到错误消息:

**线程“main”java.util.InputMismatchException中的异常:对于输入字符串:“4444”

位于java.util.Scanner.nextInt(未知源)

位于java.util.Scanner.nextInt(未知源)

位于Program1.main(Program1.java:9)**

我不确定我是否完全错误地将int转换为数组,或者它是否是小而愚蠢的东西。我在Java方面没有很多经验。有人能解释一下我做错了什么,或者有没有更好的方法来完成我想要完成的事情?任何见解都会有所帮助

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the credit number: ");
    int creditNumber = input.nextInt();

    // Converts int to array.
    String temp = Integer.toString(creditNumber);
    int [] arr = new int[temp.length()];
    int counter = 0;
    for(int i = 0; i < temp.length(); i++) {
        arr[i] = temp.charAt(i) - '0';
        counter++;
    }

    // Checks for correct amount of numbers.
    if(!(counter >= 13 && counter <= 16)) {
        System.out.println("Invalid.");
        System.exit(0);
    }   

    // Checks for type of card.
    if((arr[0] != 4) || (arr[0] != 5) || (arr[0] != 6) || ((arr[0] != 3) && (arr[1] != 7))) {
        System.out.println("Invalid");
        System.exit(0);
    }
    else
        System.out.println("Valid");
        System.exit(0);

    input.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入信用证号码:”);
int creditNumber=input.nextInt();
//将int转换为数组。
字符串temp=Integer.toString(creditNumber);
int[]arr=新int[temp.length()];
int计数器=0;
对于(int i=0;i如果(!(计数器>=13和计数器>P>44444444444444是整数中的一个数。由于输入超出整数的范围,就会得到输入失配异常。考虑用长或大整数代替。我试了很长时间,它工作得很好。

< P> 44444444444444是一个太大的数,不适合于整数。因为你的输入是BEYO。一个整数的范围,你会得到一个输入不匹配的异常。考虑使用一个长的或BigTin。我试了很长时间,它工作得很好。

谢谢,它消除了错误信息。第二个if语句仍然不能正常工作。如果没有满足这些要求,它应该打印无效,这意味着它只能满足。其中一个。但是它只打印无效。有没有办法解决这个问题?谢谢,这样就消除了错误消息。第二个if语句仍然不能正常工作。如果没有满足这些要求,它应该打印无效,这意味着它只需满足其中一个要求。但是它只打印无效。有没有办法解决这个问题?