2017-06-22 49 views
1

這是我編寫的用於查找字符串中最小單詞的代碼,但每當我嘗試在eclipse中運行它時,它都會顯示一個(字符串索引超出範圍-2147483648)嵌套while語句錯誤,我已標記,我不明白它的原因,因爲我的程序似乎在範圍內運行良好,即小於輸入字符串的長度。在java中查找字符串中的最小單詞

在此先感謝!

import java.util.Scanner; 

public class Minword { 
    public static String minLengthWord(String input){ 

     // Write your code here 
     int count[]=new int[50],i,j=0,len=input.length(); 
     String output = ""; 
     for(i=0;i<len;i++) 
     { 
      if(input.charAt(i)!=' ') 
      { 
       count[j]++; 
      } 
      else 
       j++; 
     } 
     int minidx=0; 
     for(i=1;i<j;i++) 
     { 
      if(count[minidx]>count[i]) 
       minidx=i; 
     } 
     int words=0; 
     i=0; 
     while(words<=minidx) 
     { 

      if(words==minidx) 
      { 
       ***while(i<len && input.charAt(i)!=' ')*** 
       { 
        output+=input.charAt(i); 
        i++; 
       } 
      } 
      else if(i<len && input.charAt(i)==' ') 
        words++; 
      i++; 
     } 

     return output; 


    } 
    public static void main(String[] args) { 
     Scanner s=new Scanner(System.in); 
     String input,output; 
     input=s.nextLine(); 
     output=minLengthWord(input); 

    } 

} 
+0

你的外'while'環路無限的,因爲在某些條件下'單詞'不會增加。 –

+0

你的外部while循環總是爲真,我增加直到達到最大整數值。詳細請參閱我的答案 – haifzhan

回答

0

您使用的是可變i,這是一個signed int,所以它的範圍從-2147483648到2147483647 下面的案例表明您的問題:

i = 2147483647; 
i++; 

增量後,i的由於int溢出,值將爲-2147483648。檢查這question

看來你正在得到一個巨大的輸入,因此它造成了問題。

1

我的代碼出現問題,但要獲取最短的單詞長度,您可以使用Stream和min()。你的minLengthWord方法可能是這樣的:

String f = "haha hah ha jajaja"; 
OptionalInt shortest = Arrays.stream(f.split(" ")).mapToInt(String::length).min(); 
System.out.println(shortest.getAsInt()); 
0

那麼,-2147483648是最大整數+1。你有一個環繞。變量我變得如此之大,以至於它再次從負面開始。

如果要處理大於2 GB的文本,則必須使用long。

+1

文本不大於2 GB。你可以輸入短語「你好世界」,它仍然會發生。 –

+0

原因是外環始終爲真,變量i增加到最大值 – haifzhan

0
 while(words<=minidx) 
     { 

      if(words==minidx) 
      { 
       ***while(i<len && input.charAt(i)!=' ')*** 
       { 
        output+=input.charAt(i); 
        i++; 
       } 
      } 
      else if(i<len && input.charAt(i)==' ') 
        words++; 
      i++; 
     } 

你的問題是你當文字和minidx都爲0,你的外循環,而始終是真實的和字總是等於minidx,我不斷增加,直到達到其最大數目。

你需要你的內心,而循環;其次,您需要更改i<ji<=j

以下後添加break被更正後的代碼:

int minidx = 0; 
     for (i = 1; i <= j; i++) { //-------------------------> change i<j to i<=j 
      if (count[minidx] > count[i]) 
       minidx = i; 
     } 
     int words = 0; 
     i = 0; 

     System.out.println(minidx); 
     while (words <= minidx) { 

      if (words == minidx) { 
       while (i < len && input.charAt(i) != ' ') { 
        output += input.charAt(i); 
        i++; 
       } 
       break; //-------------------------> add break statement here. 
      } else if (i < len && input.charAt(i) == ' ') { 
       words++; 
      } 
      i++; 
     } 
0

,當我試圖與運行代碼輸入「Hello World」,minidxwhile循環之前爲0。 words也是0,因此words<=minidx爲真,並輸入循環。 words==minidx爲真(它們都是0),因此輸入if語句。因爲它永遠不會進入else if(這是唯一的地方words被更改),因此words始終是0.因此,循環變成無限循環。與此同時,i只是繼續增長,直到它溢出,併成爲負面。

0

這裏有一個版本,使得采用Java 8的流API的: 從minLengthWord方法刪除所有的代碼,並粘貼下面的代碼,將工作和解決運行時的問題太

List<String> words = Arrays.asList(input.split(" ")); 

String shortestWord = words.stream().min(
            Comparator.comparing(
            word -> word.length())) 
            .get(); 

System.out.println(shortestWord); 
相關問題