Need help getting rid of [ and ] in my strings

Discussion in 'Plugin Development' started by scout109, Oct 4, 2012.

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

    scout109

    Here is the code

    String[] messages = getConfig().getString("message").split(",");
    Random rand = new Random();
    String sendit = messages[rand.nextInt(messages.length)];
    String sentit = sendit.replace("\\]\\[", "");


    When the first string is picked from the table it displays [Stringstuffhere
    and when the last string is picked it displays Stringstuffhere]
    however strings between the first and last do not have [ or ]

    I want those dang brackets gone!
     
  2. Offline

    zeeveener

    Just so I can understand better let me lay out what I can see you doing here:
    1. Get a string message from the config. "test,test2,test3,test4"
    2. Split that message into an array by breaking it at the commas. {"test","test2","test3","test4"}
    3. Using a random generator, get a message from the array.
      1. rand.nextInt(messages.length) = 2
      2. messages[2] = "test3"
    4. I would assume you are using this for other purposes but lets say you want to display it in console.
      1. log.info(sendit) --Displays-- "test3"
    You are running into an error where it shows the [ and ] on the first and last string. Thus the sentit line.

    Try instead of getting the string right away with the Random(), using an int as an index marker and getting messages[index].
    Have you tried just doing: log.info(getConfig().getString("message"); and seeing what it shouts out? That might help you figure out if the config is adding the brackets.

    Can you try that then show the output so I can get a better understanding of what you mean?
     
  3. Offline

    scout109

    If I follow you correctly, (I just got in to coding a day ago, gotten pretty far with tons of duct tape and no sleep), the config is printing the brackets after adding log.info to print the string it gave me this

    [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, x, y, z]
     
  4. Offline

    jamietech

    If you wish to use regex replace use .replaceAll. You're using a charsequence replace and that won't take out what you think it will (unless you don't use regex then it takes out exactly what you think it will).
     
  5. Offline

    scout109

    That sounds like a mouthful, anyway

    String sentit = sendit.replaceAll("\\]\\[", ""); has no effect, please let me know if I am doing this wrong :p
     
  6. Offline

    jamietech

    Try
    Code:
    sentit = sendit.replace("\\", "").replace("/", "");
     
  7. Offline

    scout109

    no effect, how exactly will [ and ] go away if there is only "\\" and "/"? :eek:

    *Code runs with no errors, however "[" and "]" still remain
     
  8. Offline

    IcyRelic

    @scout109
    sendit.replace("[", "").replace("]", "");
     
  9. Offline

    jamietech

    Oh I didn't realise you wanted the brackets gone, sorry!
     
  10. Offline

    scout109

    Its working, thanks :)

    lol :p
     
  11. Offline

    IcyRelic

    no problem
     
  12. Offline

    zeeveener

    That is odd that you even ran into this problem. Have you considered saving the strings into a StringList inside the config? Might make it longer to read but it would make it easier to use.

    So instead of:
    Code:yaml
    1. String: "test,test2,test3,test4"

    Try using:
    Code:yaml
    1. Strings:
    2. - test
    3. - test2
    4. - test3
    5. - test4
     
  13. Offline

    scout109

    I am using the second method with ' ', which is working great now
    Code:yaml
    1. Strings:
    2. - 'test'
    3. - 'test2'
    4. - 'test3'
    5. - 'test4'


    I am not sure if there is a difference between using them or not, but with .split(","); and some other issue, I am unable to put ' or , in the strings.

    - 'For example, I can't use this without getting a error at , or ' '

    I will try to use stringlist, but for hours I ran into problems with that I have know idea @.@

    Here is the code with stringlist

    List<String> messages = getConfig().getStringList("message"); //.split(","); //split no longer working
    String sendit = messages<rand.nextInt(messages.length)>; //I get errors here in eclipse, trying to solve how this will work

    farther testing shows that if I remove ' ' from the ends of each string, I am able to put ' in the strings without problems but , still remains a issue.

    I will see about trying a different split();

    Still not using StringList() since I dont know how to use that

    Well I am stumped. Anyone know how to get comma's in a string?

    Mark this as solved, I got it working great :)
     
Thread Status:
Not open for further replies.

Share This Page