2013-10-03 78 views
2

這段代碼是好的,運行良好。掃描不停止hasNextInt()

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

和運行,我的輸入掃描儀沒有停止,而對於這一點,我沒有得到任何結果。 這是怎麼回事?

請給我一個解釋說明。先謝謝你。

+0

你是什麼意思的「不停止」? –

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

} 
} 
+0

沒關係,但是當我爲這個Double值添加相同的代碼時,它不起作用。 –

+0

你嘗試過什麼?將您的代碼添加爲更新 –

0

我不同意Mr.Vihar1903 ... bcoz這個問題不是由於僅hasNext()... 它bcoz第一輸入總是去這條線 如果(Arrays.asList(關鍵字).contains(sn.next())){ 然後下一個輸入來有下一個.....最好將您的第一個控制檯輸入存儲在一個字符串變量,如String str = sc.next(); 之後,你可以比較它............

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