在制作游戏启动器/更新程序时,我遇到了一个奇怪的错误:java.lang.StringIndexOutOfBoundsException

时间:2014-12-02 00:18:15

标签: java html intellij-idea updates

在制作游戏启动器/更新程序时,我遇到了一个奇怪的错误。问题完全错误可以在这里看到:

java.lang.StringIndexOutOfBoundsException: String index out of range: -9
    at java.lang.String.substring(String.java:1911)
    at com.afroraydude.rpg.launcher.Updater.getLatestVersion(Updater.java:18)
    at com.afroraydude.rpg.launcher.RMLauncher.main(RMLauncher.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Process finished with exit code 0

这是我的代码:

RMLauncher.java

package com.afroraydude.rpg.launcher;
/**
 * Created by afroraydude.
 * This Class runs the 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 by 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();
    }
}

在制作游戏启动器/更新程序时,我遇到了一个奇怪的错误。问题完全错误可以在这里看到:

java.lang.StringIndexOutOfBoundsException: String index out of range: -9
    at java.lang.String.substring(String.java:1911)
    at com.afroraydude.rpg.launcher.Updater.getLatestVersion(Updater.java:18)
    at com.afroraydude.rpg.launcher.RMLauncher.main(RMLauncher.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Process finished with exit code 0

这是我的代码: RMLauncher.java

package com.afroraydude.rpg.launcher;
/**
 * Created by afroraydude.
 * This Class runs the 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 by 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();
    }
}

请允许我说明应该发生的事情。应用程序应该从html文件中打印版本。但相反,它给出了这个错误。我希望这对我来说是一个问题,而不仅仅是一些无法修复的随机奇怪的东西。

1 个答案:

答案 0 :(得分:1)

要查找history标记之间的值,您可以使用类似

的内容
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);
    }
}

输出

hello

就个人而言,我更喜欢使用正则表达式Pattern,如

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

输出是(也)

hello