2016-04-04 214 views
0

我有兩個字符串:如何將字符串轉換爲「[]」到一個整數數組

String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]"; 

String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]"; 

所有我想要做的就是找到這兩個字符串的最小共同數量。所以我首先取代「[]」符號,並從字符串中提取數字爲整數。然後使用一個循環來找到最小的共同number.The程序如下:

s1 = s1.replace("[",""); 
s1 = s1.replace("]",""); 
String [] band = s1.split(",");  
s2 = s2.replace("[",""); 
s2 = s2.replace("]",""); 
String [] hotel = s1.split(",");  
System.out.println(EarliestCommonSlot(hotel,band)); 

的EarliestCommonSlot()的定義如下:

public static int EarliestCommonSlot(String [] a1, String [] b1){ 
    int i=0,j=0; 
    int common = -1; 
    int [] a = new int [a1.length]; 
    int [] b = new int [b1.length]; 

    for(i = 0;i < a1.length;i++) 
    { 
     a[i] = Integer.parseInt(a1[i]); 
     System.out.println(a1[i]); 
    } 
    for(i = 0;i < b1.length;i++) 
    { 
     b[i] = Integer.parseInt(b1[i]); 
     System.out.println(b1[i]); 
    } 
    i = 0; j=0; 
    while (i< a.length && j < b.length){ 
     if (a[i] == b[j]){ 
      common = a[i]; break; 
     } 
     if (a[i] < b[j]){ 
      i++; 
     } 
     else j++; 
    } 
    return common; 
} 

但是當我運行該程序,它具有以下錯誤:

Exception in thread "main" java.lang.NumberFormatException: For input string: " 143" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:481) 
    at java.lang.Integer.parseInt(Integer.java:527) 
    at ClientReserve.EarliestCommonSlot(ClientReserve.java:39) 
    at ClientReserve.main(ClientReserve.java:179) 

這是爲什麼?我怎麼能解決這個問題?

+2

領先的空間是問題所在。嘗試''Integer.parseInt(a1 [i] .trim());''或確保分割的字符串不包含任何空格。 – f1sh

回答

6

而不是

s1.replace("]"," "); // This replaces the bracket with a space. 
        // The number with the space will emit the error 

用途:

s1.replace("]","");//This replaces the bracket with an empty string. 

新代碼:

s1 = s1.replace("[",""); 
s1 = s1.replace("]",""); 
String [] band = s1.split(", ");  
s2 = s2.replace("[",""); 
s2 = s2.replace("]",""); 
String [] hotel = s1.split(", "); //Comma and a space. Thanks to SaviourSelf 
System.out.println(EarliestCommonSlot(hotel,band)); 
+0

感謝您的回覆,但不起作用。 – NUO

+0

@NUO使用'.trim()'從字符串中刪除前導和尾隨空格。 –

+0

@NUO對於兩個字符串's1'和's2'以及兩個括號,您必須使用replace(「]」,「」);'。 – Hackerdarshi

1

請刪除陣列字符串中的空格。這可以通過

st.replaceAll("\\s+","")st.replaceAll("\\s","")

在錯誤本身顯示出143具有前綴空間來完成。 異常線程 「main」 java.lang.NumberFormatException:對於輸入字符串: 「143」

0
public class Test { 


    public static void main(String[] args) { 



String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]"; 

String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]"; 
     s1 = s1.replace("[",""); 
     s1 = s1.replace("]",""); 
     s1 = s1.replace(" ",""); 
     String [] band = s1.split(",");  
     s2 = s2.replace("[",""); 
     s2 = s2.replace("]",""); 

     String [] hotel = s1.split(",");  
     System.out.println(EarliestCommonSlot(hotel,band)); 
    } 
    public static int EarliestCommonSlot(String [] a1, String [] b1){ 
     int i=0,j=0; 
     int common = -1; 
     int [] a = new int [a1.length]; 
     int [] b = new int [b1.length]; 

     for(i = 0;i < a1.length;i++) 
     { 
      a[i] = Integer.parseInt(a1[i]); 
      System.out.println(a1[i]); 
     } 
     for(i = 0;i < b1.length;i++) 
     { 
      b[i] = Integer.parseInt(b1[i]); 
      System.out.println(b1[i]); 
     } 
     i = 0; j=0; 
     while (i< a.length && j < b.length){ 
      if (a[i] == b[j]){ 
       common = a[i]; break; 
      } 
      if (a[i] < b[j]){ 
       i++; 
      } 
      else j++; 
     } 
     return common; 
    }  
    } 
1

使用TRIM()方法

String s1 = "[143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 162, 163, 164]"; 

    String s2 = "[20, 35, 74, 78, 124, 125, 126, 127, 131, 132, 143, 144, 145, 146]"; 
    s1 = s1.replace("[",""); 
    s1 = s1.replace("]",""); 
    String [] band = s1.split(",");  
    s2 = s2.replace("[",""); 
    s2 = s2.replace("]",""); 
    String [] hotel = s1.split(",");  
    System.out.println(EarliestCommonSlot(hotel,band)); 



public static int EarliestCommonSlot(String [] a1, String [] b1){ 
     int i=0,j=0; 
     int common = -1; 
     int [] a = new int [a1.length]; 
     int [] b = new int [b1.length]; 

     for(i = 0;i < a1.length;i++) 
     { 
      a[i] = Integer.parseInt(a1[i].trim()); 
      System.out.println(a1[i]); 
     } 
     for(i = 0;i < b1.length;i++) 
     { 
      b[i] = Integer.parseInt(b1[i].trim()); 
      System.out.println(b1[i]); 
     } 
     i = 0; j=0; 
     while (i< a.length && j < b.length){ 
      if (a[i] == b[j]){ 
       common = a[i]; break; 
      } 
      if (a[i] < b[j]){ 
       i++; 
      } 
      else j++; 
     } 
     return common; 
    } 

我遇到這種方法和它的工作

相關問題