2016-11-06 97 views
1

下面的代碼塊顯示此錯誤jelliot「運行Exception-功能尚未實現」,當它到達線運行Exception-功能尚未實現

char arr [] = w.toCharArray(); 

而在其他的編譯器,它贏得了」 t取其應該輸入的數量。如果我設置n = 4,它只需要2個輸入。

Scanner Sc = new Scanner (System.in); 
    int n = Sc.nextInt(); 
    for(int count = 1;count<=n; count++){ 
     String w = Sc.nextLine(); 
     char arr [] = w.toCharArray(); 
     if(arr.length > 4){ 
      System.out.print(arr[0]); 
      System.out.print(arr.length-2); 
      System.out.print(arr[arr.length-1]); 
     } 
     else{ 
      System.out.println(w); 
     } 
     System.out.println(); 
    } 
+0

'如果我設置n = 4,它只需要2個輸入.'好像需要3個,你正在使用哪個java版本,不支持'CharArray'? –

+0

你能解釋一下「jeliot」是什麼(以及你正在使用哪個版本)?看起來像是一些圖書館/應用程序不完全支持你想要做的事情。 –

+0

它被設置爲計數<= n,不計數

回答

0

的異常指示String類的這種特殊的方法(toCharArray())爲Java的(大概非標)實現您所使用未實現。您可以通過不使用char數組來解決此問題,而是使用String方法length()charAt()。試試這個:

Scanner Sc = new Scanner (System.in); 
int n = Sc.nextInt(); 
for(int count = 1;count<=n; count++){ 
    String w = Sc.nextLine(); 
    if(w.length() > 4){ 
     System.out.print(w.charAt(0)); 
     System.out.print(w.length()-2); 
     System.out.print(w.chatAt(w.length()-1)); 
    } 
    else{ 
     System.out.println(w); 
    } 
    System.out.println(); 
} 
+0

明白了!謝謝! –