Reading specified words in a string.

Discussion in 'Plugin Development' started by DeadlyScone, Sep 18, 2011.

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

    DeadlyScone

    my greatest apologies if this is very easy, i was never very good at IO operations.
    -
    okay, i need to read a couple words from a given string. Lets say the string is X:1 Y:2 Z:3
    i want to create 3 variables for each X, Y, Z
    int x;
    int y;
    int z;
    and retrieve the number after the colon <:> for each coordinate and set the variable accordingly.
    btw i'am using FileReader and BufferedReader, Is this correct?

    P.S: i have not a clue how to do this beyond setting up FileReader and BufferedReader for the correct paths, so any help would be awesome possum.

    please let me know, i do enjoy comments :)
     
  2. Offline

    ItsHarry

    Something like:
    Code:
    String s = "x:1,y:2,z:3";
    
    String[] ss = s.split(",");
    
    String[] xx = ss[0].split(":");
    
    String[] yy = ss[1].split(":");
    
    String[] zz = ss[2].split(":);
    
    int x = Integer.parseInt(xx[1]);
    
    int y = Integer.parseInt(yy[1]);
    
    int z = Integer.parseInt(zz[1]);
     
  3. Offline

    DeadlyScone

    -
    IO is wierd lol, can't they just make it so you can select a line number or read past char to char.. hmm anyway i think this might work. give it a try later.
     
  4. Offline

    ItsHarry

    They're other ways to do this.
    E.g. instead of saving it as a text, you could put all the stuff in a hashmap
     
  5. Offline

    DeadlyScone

    ok so lets say i have multiple lines,
    ex:

    x:1,y:2,z:3
    x:1,y:2,z:3
    x:1,y:2,z:3
    x:1,y:2,z:3
    how do i get it to read a specific line number?
     
  6. If the values are always going to be in the same order, (X,Y,Z) then you don't need to store the letter.

    Code:
    1,2,3
    1,2,3
    1,2,3
    1,2,3
    You could read every line like this:
    Code:
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        if (!line.isEmpty()) {
            split = line.split(",");
            int x = (int)split[0];
            int y = (int)split[1];
            int z = (int)split[2];
    
            //Probably best to store them in memory somehow, using a HashMap maybe.
        }
    }
    To read a specific line, I'm not sure, I think you would have to read through them all untill you came to the one you wanted.
     
  7. Offline

    ItsHarry

    Best option would still be to use hashmaps.
     
  8. Well, since he is loading it from a file I guess it is something which needs to be saved while the server is not running, so HashMaps alone cant be used. But reading the file repeatedly would be stupid, hence I recommended loading the data into some sort of storage in memory (possibly a HashMap).

    Edit: Oh you mean because a HashMap is serialisable, so means you don't have to load it all in like this?
    That is true I guess, but he didn't say the data comes from the running program, it might need to be edited in text form.
     
  9. Offline

    ItsHarry

    Yep, I mean serializble
     
  10. Offline

    DeadlyScone

    -
    hmm, thanks ill have to use a hashmap i guess (never worked with them) ill go look at some more forms that discuss hashmaps.

    it
    Well i'am trying to store the task id of a scheduler, so i guess i should not use a file in that case. The scheduler is started when someone flips a lever and needs to cancel when its turned off using the task id that it used when it started. i figured storing the task id would be sufficient.
    do you any thoughts of how to go about this?

    EDIT: the only reason i don't just use cancelsynctask is because there will be allot of levers and i only want to cancel the one that is turned off.

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

Share This Page