Using substring and charAt

Discussion in 'Plugin Development' started by KeybordPiano459, Feb 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    KeybordPiano459

    I'm having trouble, whenever I try to read from a URL with this code:
    Code:java
    1. URL url = new URL("[URL]https://raw.github.com/keybordpiano459/Newspaper/master/version.txt[/URL]");
    2. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    3. String string;
    4. while ((string = br.readLine()) != null) {
    5. String line = br.readLine();
    6. if (line.charAt(0) == '#') return;
    7. if (line.substring(0, 4).equals(currentVersion + ": "))
    8. plugin.updatemsg = line.substring(5);
    9. log.info(plugin.updatemsg);
    10. }
    11. br.close();

    I get this stacktrace:
    Code:
    08:50:18 [SEVERE] Error occurred while enabling Newspaper v1.3 (Is it up to date?)
    java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:658)
        at me.KeybordPiano459.Newspaper.UpdateChecker.startUpdateCheck(UpdateChecker.java:29)
        at me.KeybordPiano459.Newspaper.Newspaper.onEnable(Newspaper.java:35)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_4_R1.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_4_R1.CraftServer.enablePlugins(CraftServer.java:264)
        at net.minecraft.server.v1_4_R1.MinecraftServer.j(MinecraftServer.java:321)
        at net.minecraft.server.v1_4_R1.MinecraftServer.e(MinecraftServer.java:300)
        at net.minecraft.server.v1_4_R1.MinecraftServer.a(MinecraftServer.java:259)
        at net.minecraft.server.v1_4_R1.DedicatedServer.init(DedicatedServer.java:149)
        at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:399)
        at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Any idea what's wrong? I'm pretty sure that when using charAt and substring, you're supposed to start at 0.

    Btw, there aren't supposed to be [.url] tags in the first syntax thingy.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. it seems there is n empty line inside the file, check if the string is empty by using .isempty
     
  3. Offline

    KeybordPiano459

    I already try checking if the line is null or not, wouldn't that do it?

    By changing the while to loop to this, it didn't make a stacktrace, however, it doesn't print anything past "Checking for a new version..."
    Code:java
    1. while (br.readLine() != null && !br.readLine().isEmpty()) {

    When I replace && with ||, it just makes the stacktrace again =/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Code:java
    1.  
    2. while ((string = br.readLine()) != null) {
    3. String line = br.readLine();
    4.  

    I think that should be
    Code:java
    1.  
    2. while ((string = br.readLine()) != null) {
    3. String line = string;
    4.  
     
  5. Offline

    KeybordPiano459

    ferrybig
    Now I have
    Code:java
    1. String string;
    2. while ((string = br.readLine()) != null) {
    3. String line = string;

    And all it does is print "Checking for a new update..."
    Why doesn't this work?
    Code:java
    1. while (br.readLine() != null || !br.readLine().isEmpty()) {
    2. String line = br.readLine();
     
  6. the lower line read 3 different lines, the top line reuses the same same, so the cursor only advances 1 line eacdh operation
     
  7. Offline

    KeybordPiano459

    Well ferrybig
    I did some debugging, and seems that the while loop only prints once, and I think that's because it thinks the second line is null. If you goto the URL, it is, so I'm going to try to fix it :)

    ferrybig
    It seems like the blank line wasn't the case, you can look at the edited .txt file now, and the while loop still only prints once. There's no blank line in the file anymore.

    Anyhow, I fixed it on my own. If anyone is curious how, here's the code:
    Code:java
    1. URL url = new URL("[URL]https://raw.github.com/keybordpiano459/Newspaper/master/version.txt[/URL]");
    2. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    3. String str;
    4. while ((str = br.readLine()) != null) {
    5. String line = str;
    6. if (line.charAt(0) == '#');
    7. if (line.charAt(0) == '1' && line.charAt(2) == '3') {
    8. plugin.updatemsg = line.substring(5);
    9. log.info(plugin.updatemsg);
    10. }
    11. }
    12. br.close();


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page