2016-04-04 8 views
0

Ich habe zwei Strings:Wie eine Zeichenfolge mit „[]“ in eine Integer-Array konvertieren

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

Alles, was ich tun möchte, ist die kleinste gemeinsame Nummer in diesen beiden Strings zu finden. Also verdränge ich zuerst das Symbol "[]" und ziehe Zahlen aus der Zeichenkette in ganze Zahlen. Dann eine Schleife verwenden, das kleinste gemeinsame number.the Programm zu finden, ist wie folgt:

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

Die EarliestCommonSlot() definiert ist, wie folgt:

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

Aber wenn ich das Programm ausführen, es das hat folgender Fehler:

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) 

Warum ist das? Wie könnte ich das beheben?

+2

Der führende Platz ist das Problem. Probieren Sie '' Integer.parseInt (a1 [i] .trim()); '' oder stellen Sie sicher, dass die geteilten Zeichenfolgen keine Leerzeichen enthalten. – f1sh

Antwort

6

Statt

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

Verwendung:

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

Neuer Code:

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

Vielen Dank für Ihre Antwort, aber es funktioniert nicht. – NUO

+0

@NUO Verwenden Sie '.trim()', um führende und nachstehende Leerzeichen aus Strings zu entfernen. –

+0

@NUO Sie müssen 'replace ("] "," ");' für beide Strings 's1' und' s2' und für beide Klammern verwenden. – Hackerdarshi

1

Bitte entfernen Sie die weißen Flecken von den Saiten in Array. Das kann durch

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

in dem Fehler geschehen selbst es zeigt, dass 143 Raum in Präfix haben. Exception in thread "main" java.lang.NumberFormatException: Für Eingabezeichenfolge "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

verwenden die Trimmung() Methode

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

ich diese Methode ausgeführt haben und es funktionierte

Verwandte Themen