1

このコードは問題なく、うまく動作します。

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

実行すると、入力スキャナーが停止しません。そのため、結果が得られません。なぜこうなった?

説明付きの解決策を教えてください。前もって感謝します。

4

3 に答える 3

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

}
}
于 2013-10-03T04:53:07.733 に答える
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");
            }
        }
    }
}
于 2013-10-03T05:31:42.133 に答える