Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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
Java hasNextInt()的扫描未停止_Java_Java.util.scanner - Fatal编程技术网

Java hasNextInt()的扫描未停止

Java hasNextInt()的扫描未停止,java,java.util.scanner,Java,Java.util.scanner,这段代码运行良好 public static void main(String[] args) { String [] keywords={"auto","break","case","char","const","continue","default", "do","double","else","enum","extern","float","for","goto","if","int", "long","register","retu

这段代码运行良好

public static void main(String[] args) {
    String [] keywords={"auto","break","case","char","const","continue","default",
            "do","double","else","enum","extern","float","for","goto","if","int",
            "long","register","return","short","signed","sizeof","static","struct",
            "switch","typedef","union","unsigned","void","volatile","while" };
    Scanner sn = new Scanner (System.in);
     if(Arrays.asList(keywords).contains(sn.next())) {
            System.out.println("This is a KEYWORD");
        }
但是当我加上这个

else if(sn.hasNextInt()){
         System.out.println("This is an INTEGER");
     }
然后运行,我的输入扫描仪没有停止,因此,我没有得到任何结果。 为什么会这样


请给我一个描述的解决方案。提前谢谢。

我不同意维哈先生的看法。。。
import java.util.Arrays;
import java.util.Scanner;


public class jh {

    public static void main(String[] args) {
        String [] keywords={"auto","break","case","char","const","continue","default",
                "do","double","else","enum","extern","float","for","goto","if","int",
                "long","register","return","short","signed","sizeof","static","struct",
                "switch","typedef","union","unsigned","void","volatile","while" };
        Scanner sn = new Scanner (System.in);
        /* get the value from scanner.. do not scan a single input twice.
         In your code in line Arrays.asList(keywords).contains(sn.next())) {, you have 
         already got the input once. If you had tried entering another integer after that and you
         should have got the This is an INTEGER
        */
        String string = sn.next();
        Integer someInt = null;
        try{//see if the input was an integer 
         someInt= Integer.parseInt(string);
        }catch(NumberFormatException e){System.out.println(e);}

         if(Arrays.asList(keywords).contains(string)) {
                System.out.println("This is a KEYWORD");
            }
         else if(someInt!=null){
             System.out.println("This is an INTEGER");
         }

}
}
bcoz此问题不仅仅是因为hasNext()。。。 这是bcoz的第一个输入,请始终转到此行 if(Arrays.asList(keywords).contains(sn.next())){ 然后next input come to有next…..最好将您的第一个控制台输入存储在字符串变量中,如string str=sc.next();
然后你可以比较一下我不同意维哈先生的观点。。。
/**
 *
 * @author hjayamanna001
 */
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        String[] keywords = {"auto", "break", "case", "char", "const", "continue", "default",
            "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
            "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
            "switch", "typedef", "union", "unsigned", "void", "volatile", "while"};
        String s = "";
        int i = 0;
        while (true) {
            System.out.println("--------------------");
            System.out.println("Input a character:  ");
            Scanner sn = new Scanner(System.in);
            s = sn.next();
            try {
                if (Arrays.asList(keywords).contains(s)) {
                    System.out.println("This is a KEYWORD");

                } else {
                    i = Integer.parseInt(s);
                    System.out.println("This is an INTEGER");
                }
            } catch (Exception e) {
                System.out.println("This is not MATCHING");
            }
        }
    }
}
bcoz此问题不仅仅是因为hasNext()。。。 这是bcoz的第一个输入,请始终转到此行 if(Arrays.asList(keywords).contains(sn.next())){ 然后next input come to有next…..最好将您的第一个控制台输入存储在字符串变量中,如string str=sc.next();
然后你可以比较它…………

你说的“不停止”是什么意思?不停止是指,它没有给我任何输出,仍然扫描输入。你说的“不停止”是什么意思?不停止的意思是,它没有给我任何输出,仍然扫描输入。这没关系,但当我用这个代码添加相同的代码作为双值时,它不起作用。这没关系,但当我用这个代码添加相同的代码作为双值时,它不起作用。
/**
 *
 * @author hjayamanna001
 */
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        String[] keywords = {"auto", "break", "case", "char", "const", "continue", "default",
            "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
            "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
            "switch", "typedef", "union", "unsigned", "void", "volatile", "while"};
        String s = "";
        int i = 0;
        while (true) {
            System.out.println("--------------------");
            System.out.println("Input a character:  ");
            Scanner sn = new Scanner(System.in);
            s = sn.next();
            try {
                if (Arrays.asList(keywords).contains(s)) {
                    System.out.println("This is a KEYWORD");

                } else {
                    i = Integer.parseInt(s);
                    System.out.println("This is an INTEGER");
                }
            } catch (Exception e) {
                System.out.println("This is not MATCHING");
            }
        }
    }
}