html - While making a game launcher/updater I encountered a strange error: java.lang.StringIndexOutOfBoundsException -


while making game launcher/updater encountered strange error. issue full error can seen here:

java.lang.stringindexoutofboundsexception: string index out of range: -9     @ java.lang.string.substring(string.java:1911)     @ com.afroraydude.rpg.launcher.updater.getlatestversion(updater.java:18)     @ com.afroraydude.rpg.launcher.rmlauncher.main(rmlauncher.java:10)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:606)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:134)  process finished exit code 0 

this code had:

rmlauncher.java:

package com.afroraydude.rpg.launcher; /**  * created afroraydude.  * class runs game */ public class rmlauncher { public static void main(string[] args) throws exception {     string[] cmds;     cmds = new string[]{"game.exe", "custom_launcher"};     try {         system.out.println(updater.getlatestversion());         } catch (exception ex) {             ex.printstacktrace();     }     //runtime.getruntime().exec(cmds).waitfor();     } } 

updater.java:

package com.afroraydude.rpg.launcher;  import java.io.inputstream; import java.net.url;  /**  * created afroraydude  */ public class updater {      private final static string versionurl = "http://afroraydude.com/rpgupdate/version.html";      private final static string historyurl = "";      public static string getlatestversion() throws exception     {         string data = getdata(versionurl);         return data.substring(data.indexof("[version]")+9,data.indexof("[/version]"));     }     public static string getwhatsnew() throws exception     {         string data = getdata(historyurl);         return data.substring(data.indexof("[history]")+9,data.indexof("[/history]"));     }     private static string getdata(string address)throws exception     {         url url = new url(address);          inputstream html = null;          html = url.openstream();          int c = 0;         stringbuffer buffer = new stringbuffer("");          while(c != -1) {             c = html.read();              buffer.append((char)c);         }         return buffer.tostring();     } } 

while making game launcher/updater encountered strange error. issue full error can seen here:

java.lang.stringindexoutofboundsexception: string index out of range: -9     @ java.lang.string.substring(string.java:1911)     @ com.afroraydude.rpg.launcher.updater.getlatestversion(updater.java:18)     @ com.afroraydude.rpg.launcher.rmlauncher.main(rmlauncher.java:10)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:606)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:134)  process finished exit code 0 

this code had: rmlauncher.java

package com.afroraydude.rpg.launcher; /**  * created afroraydude.  * class runs game */ public class rmlauncher { public static void main(string[] args) throws exception {     string[] cmds;     cmds = new string[]{"game.exe", "custom_launcher"};     try {         system.out.println(updater.getlatestversion());         } catch (exception ex) {             ex.printstacktrace();     }     //runtime.getruntime().exec(cmds).waitfor();     } } 

updater.java

package com.afroraydude.rpg.launcher;  import java.io.inputstream; import java.net.url;  /**  * created afroraydude  */ public class updater {      private final static string versionurl = "http://afroraydude.com/rpgupdate/version.html";      private final static string historyurl = "";      public static string getlatestversion() throws exception     {         string data = getdata(versionurl);         return data.substring(data.indexof("[version]")+9,data.indexof("[/version]"));     }     public static string getwhatsnew() throws exception     {         string data = getdata(historyurl);         return data.substring(data.indexof("[history]")+9,data.indexof("[/history]"));     }     private static string getdata(string address)throws exception     {         url url = new url(address);          inputstream html = null;          html = url.openstream();          int c = 0;         stringbuffer buffer = new stringbuffer("");          while(c != -1) {             c = html.read();              buffer.append((char)c);         }         return buffer.tostring();     } } 

allow me state should happening. application should print version html file. instead giving error. hope issue on part , not random weird thing can't fixed.

to find value between history tag, use like

string data = "<history>hello</history>"; int openpos = data.indexof("history"); if (openpos >= 0) {     int closepos = data.indexof("/history", openpos + 1);     if (closepos >= 0) {         string str = data.substring(openpos + 8, closepos - 1);         system.out.println(str);     } } 

output

hello 

personally, prefer use regular expression pattern like

string data = "<history>hello</history>"; pattern p = pattern.compile("<history>(\\s+)</history>",         pattern.case_insensitive); matcher m = p.matcher(data); if (m.matches()) {     system.out.println(m.group(1)); } 

output (also)

hello 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -