2013-05-24 4 views
0

J'ai quelques problèmes pour faire fonctionner une méthode correctement en Java. Dans le programme, j'ai créé un tableau composé de nombreux mots différents de longueur différente. La méthode sur laquelle je travaille est censée prendre les 3 premières lettres d'une chaîne. J'utilise obj.substring (0,3). Mais je reçois StringIndexOutOfBoundException. Quelqu'un pourrait-il m'aider s'il vous plaît à résoudre ce problème. voici mon code:getting exception StringIndexOutOfBoundsException

  private void singleCurrencyAsynctaskCaller() { 
    isSingleCurrencyResultOk = false; 
    String from = subSpinner1.getSelectedItem().toString().trim(); 
    String to = subSpinner2.getSelectedItem().toString().trim(); 
    String from1 = from.substring(0, 3); 
    String to1 = to.substring(0, 3); 

    if (!from1.equals(to1)) { 
     String RESTURL = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22" 
       + from1 
       + to1 
       + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="; 

     AsyncTaskSingleCurrency singleCurrencyAsyncTask = new AsyncTaskSingleCurrency(
       ConverterActivity.this, 
       new FetchSingleCurrencyTaskCompletedListner()); 
     singleCurrencyAsyncTask.execute(RESTURL); 

    } else { 

     currencySingleRate = 1.0; 
     isSingleCurrencyResultOk = true; 
     callCurrencyLogic(true); 
    } 
} 

J'ai eu cette exception:

  java.lang.StringIndexOutOfBoundsException in java.lang.String.substring 

      java.lang.StringIndexOutOfBoundsException 
      at java.lang.String.substring(String.java:1579) 
      at com.zyksa.converter. 
     ConverterActivity.singleCurrencyAsynctaskCaller(ConverterActivity.java:402) 
     at com.zyksa.converter.ConverterActivity.onClick(ConverterActivity.java:580) 
     at android.view.View.performClick(View.java:2408) 
     at android.view.View$PerformClick.run(View.java:8816) 
     at android.os.Handler.handleCallback(Handler.java:587) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:123) 
     at android.app.ActivityThread.main(ActivityThread.java:4627) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:521) 

Répondre

0

Peut-être la longueur String ne suffit pas. Essayez ceci:

String yourString; 
. 
. 
String subString; 
if (yourString.length() > 3) 
    subString = yourString.substring(0, 3); 
else 
    // Do something else 

Dans votre cas

String from1, to1; 
if(form.length() > 3) 
    from1 = from.substring(0, 3); 
else 
    // Do somthing else 

if(to.length() > 3) 
    to1 = to.substring(0, 3); 
else 
    // Do somthing else 
+0

Merci pour votre aide en temps opportun – pavi

+0

votre accueil. :) Si cette réponse vous a aidé, alors SVP la marquer comme acceptée. –

0

Essayez ceci:

String from1 = from.substring(0, 2); 
String to1 = to.substring(0, 2); 
Questions connexes