hasNextInt()的扫描没有停止

时间:2013-10-03 04:37:14

标签: 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","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");
     }

然后运行,我的输入扫描仪没有停止,为此,我没有得到任何结果。 为什么会这样?

请给我一个解释说明。提前谢谢。

3 个答案:

答案 0 :(得分:1)

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");
         }

}
}

答案 1 :(得分:0)

我不同意Mr.Vihar1903 ...... bcoz这个问题不是因为只有hasNext()...... 这是bcoz第一次输入总是去这一行     if(Arrays.asList(keywords).contains(sn.next())){ 然后下一个输入有下一个.....更好地将你的第一个控制台输入存储在字符串变量中,如String str = sc.next(); 之后你可以比较它............

答案 2 :(得分:0)

/**
 *
 * @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");
            }
        }
    }
}