2016-04-04 17 views
0

içine iki dizeleri vardır:"[]" bir dize dönüştürmek için nasıl bir tamsayı dizisi

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]"; 

bu iki dizeleri en küçük ortak sayısını bulmak yapmak istediğim. Bu yüzden ilk önce "[]" sembolünü değiştiririm ve dizelerden sayıları tamsayılara ayıkladım. Sonra en küçük ortak number.The programı bulmak için bir döngü kullanmak aşağıdaki gibidir: Ben programı çalıştırdığınızda Ancak, sahip

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

: aşağıdaki gibi

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() tanımlanır takip eden hata:

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) 

Neden? Bunu nasıl düzeltebilirim? Bunun yerine

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

Kullanım

+2

Önde gelen alan sorun. Integer.parseInt (a1 [i] .trim()); 'yi deneyin veya bölünmüş dizelerin herhangi bir boşluk içermediğinden emin olun. – f1sh

cevap

6

:

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

Yeni kod:

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

Cevabınız için teşekkür ederiz, ama işe yaramıyor. – NUO

+0

@NUO Öncü ve sondaki boşlukları Dizelerden kaldırmak için .trim() işlevini kullanın. –

+0

@NUO 's1' ve' s2' dizilerinin her ikisi için ve her iki parantez için 'replace ("] "," ");' kullanmak zorundasınız. – Hackerdarshi

1

dizideki dizeleri beyaz boşlukları kaldırın. Yani 143 öneki boşluk sahip olduğunu gösteriyor hata kendisinde

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

yapılabilir. İplik "ana" java.lang.NumberFormatException içinde İstisna: Giriş dizesi için: "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

kullanmak ben bu yöntemi çalıştırmak ve o

çalıştı

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

Döşeme() yöntemi

İlgili konular