2017-09-04 1 views
-8
public class Test { 

    public static void main(String args[]) 
    { 
     String[] test={"this","this2"}; 
     new Test().it(test); 
    } 

    public int it(String[] name) 
    { 
     return Integer.parseInt(name[0]); 
    } 
}  

Je souhaite modifier Tableau chaîne pour String.I obtenir l'exception comme cette exception dans le thread « principal » java.lang.NumberFormatException: Pour chaîne d'entrée: « this » // Changement de chaîne = nom [0]; // Renvoie Integer.parseInt (change);Comment puis-je changer tableau corde à

ce code ne fait que résoudre le problème?

+0

vous utilisez chaîne ce pour analyser en entier – Yogesh

+0

Vous devez analyser à chaîne pas int – soorapadman

+2

Essayez ceci si vous voulez vraiment convertir la chaîne ' String res = Arrays.toString (array); ' – soorapadman

Répondre

0

Vous pouvez essayer le code ci-dessous pour convertir tableau de chaînes à chaîne

public class JavaApplication20 { 

    public static void main(String[] args) { 
     // TODO code application logic here 

     String test1 = ""; 
     String[] test = {"this","this2"}; 
     int length = test.length; 
     //System.out.println("Length :"+length); 
     for(int i=0; i<length; i++){ 
      //System.out.println(test[i]); 
      test1 += test[i]+","; 
     } 
     int len = test1.length(); 
     String str = test1.substring(0, len-1); 
     System.out.println("String :"+str); 
    } 

} 
+0

merci pour la commande. mais je suis prêt à utiliser ce code. mais c'est trop long pour moi. Je souhaite utiliser une seule ligne de commande. ta réponse est très bonne. – charsLEE