Results 1 to 2 of 2
  1. #1
    nirorg is offline Member
    Join Date
    Jun 2013
    Rep Power

    Default Read the index of a string

    Hello, i'm sry for my bad english, it's my second language.
    I'm making a Java JFrame game, and i made a text file for each NPC, that a class should read, for example, this is a line of a NPC text file (This is not java, this is a text file which the program reads) :
    Java Code:
    showText("The time is 00:#NumbMode(getInt({0002},"VALUE"),2)# right#LN#now?");
    in this example, NumbMode is a command that makes 2 letters from the VALUE {0002} (the Value array is in the java code), so if the integer is 2, it will return 02, and the text will be:
    The time is 00:02 right
    (LN command is for a new line).
    so, the question is: how can i let the program read this line, i used
    Java Code:
    		command[0] = in.substring(in.indexOf("(")+1, in.indexOf(","));
    		command[1] = in.substring(in.indexOf(",")+1, in.lastIndexOf(")"));
    in order to get the first and the second arguments, but in this example, this will return:
    Java Code:
    First argument: getInt({0002}
    Second argument: "VALUE"),2
    while this is wrong, it should return:
    Java Code:
    First argument:getInt({0002},"VALUE")
    Second argument:2
    i hope you understood, this pretty much complicated, there is any way to let it return the right arguments?
    Thank you so much!! :)
    Last edited by nirorg; 06-25-2013 at 02:44 PM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    CA, USA
    Rep Power

    Default Re: Read the index of a string

    Well first of all, have you thought about saving the data in a much simpler string? Say, something separated by semicolons.
    showText;The time is 00;getInt;0002;"VALUE";2;...
    Might be easier to parse for whatever it is you're trying to do there.

    Otherwise, you may need some more advanced parsing methods, perhaps even tokenizing the string. But, if the format will always be more or less the same, you could find the second index of a comma. Or, interate through the characters of the string keeping track of when you are "inside quotation marks" and (how many (parentheses) deep you are at any given time), and extract tokens that way. Or complex regular expressions might be the answer.

    If it's your program and you have complete control over how the data is saved, I strongly recommend you save it in a form that's easier to parse.


    So I'm pondering it a bit more
    Strip away the # markers and the function name "NumbMode" as well as the exterior ( and )
    first arg is up til the first end parenthesis, second arg is after that
    Might help.
    Last edited by AndrewM16921; 06-26-2013 at 04:33 AM.

Similar Threads

  1. String index doubled
    By hill0093 in forum New To Java
    Replies: 1
    Last Post: 06-09-2013, 11:06 AM
  2. Replies: 1
    Last Post: 10-09-2011, 07:57 PM
  3. how to read array index
    By aconti in forum New To Java
    Replies: 5
    Last Post: 08-12-2011, 10:52 PM
  4. String index out of range: -1 Please Help
    By davetheant in forum New To Java
    Replies: 22
    Last Post: 09-11-2010, 01:32 AM
  5. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts