[SOLVED]ArrayIndexOutOfBoundsException, omg way to go IO

Discussion in 'Plugin Development' started by DeadlyScone, Nov 1, 2011.

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

    DeadlyScone

    so i'am trying to read dates printed as " -11/1/11 " and there is some other stuff in the text file that the BufferedReader is reading too, which for example " [Event Time : 2:10:29AM]
    [/127.0.0.1] deadlyscone has Broken SAND at X: -141 Y: 71 Z: 217 "

    okay so my guess is that BufferReader counts characters as ()[]/\ and spaces as characters that is uses for special operations. do you have to escape these? or is there some other way? or is String the wrong Object to use?
    i'am lost.
    here are the snippets.

    Code:
        BufferedReader buffbR = new BufferedReader(blockReader);
        String line;
    
        try{
        while ((line = buffbR.readLine()) != null) {
            if (!line.isEmpty()) {
                String [] split = line.split("-");
                String loggedDate = (String)split[1];
                log.info(loggedDate);
            }
        }
        }catch (Exception e1){
            e1.printStackTrace();
        }
    and blockReader is a FileReader reading from a txt file from a given path;
    log is a Logger;

    **************************************************************************
    basically i just want it to read after the given delimiter, which is the dates :/
     
  2. Offline

    Lolmewn

    I think a split[1] gives you the error.
    If you are reading from a file, dont forget to check if the file starts with a # (If using Properties)
     
  3. Offline

    DeadlyScone

    yes, that is the line that throws the error.. and i don't have any "#" in the file... but do i still need to check even if none exist? if so, how?
     
  4. Offline

    Lolmewn

    if(string.contains("-")){
    yourstuff
    }
     
  5. String.split() requires you to enter a RegEx and "-" has special meaning in Regular Expression. To use it normally you need to escape it

    This is how your line should look like:
    Code:java
    1. String [] split = line.split("\-");
     
  6. Offline

    DeadlyScone

    thanks you just solved my problem %)
     
  7. Offline

    Lolmewn

    No problem ;) Glad to help =D
     
  8. Offline

    DeadlyScone

    i think you have to double escape, and anyway it will not work.
     
  9. nope. You should really read some articles/boosk/whatever you want about RegExp.
    In java you escape it with "\", java then does internally make it to "\\". And why shouldn't it work?
    Btw for your task a special RegEx would fit best imo to just filter out the part you want to have. RegExp are very powerfull(I have build my clalculator for bukkit [SimpleCalc] with those, to filter any math expression) but sometimes a little bit hard to understand.
     
  10. Offline

    DeadlyScone

    i have read this java help website about split and how to use escape to read some pretty complex strings, and grouping numbers into say like 3's... but all in all i got the code to work with what LolMewn said.
    thanks anyway though. ill try to remember you if i have questions about regexp :D
     
  11. A little bit late but i hope thats ok. Yes if you pass the expression as a String(like this: "Hello i am a String.") to the java engine you need to 'double' escape it. If you pass it directly with java methods you only need one \ :)
     
  12. using an scanner object is also usefull
     
Thread Status:
Not open for further replies.

Share This Page