Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Parsing a text file in Java

  • 08-03-2006 3:14pm
    #1
    Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,325 Mod ✭✭✭✭


    I need a bit of help parsing a text file in Java as its been a while since I have done it. I have a text file that contains lines of input like the following.
    write[12,43]
    write[15,43]
    read[12,67]
    commit[12]
    abort[15]
    

    I need to set 3 variables. One as the operation name part (write, commit etc), one for the first number and one for the second (when required).

    Could someone please point me in the right direction as to how to do this? I know it could be done much easier using PERL but it must be done in JAVA.

    Thanks


Comments

  • Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,325 Mod ✭✭✭✭Kingp35


    Can you use regular expressions in java like in perl and if so, how?


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    well I take it you know how to read in the values from a text file so all that is need is your regular expression. This tutorial should help http://java.sun.com/docs/books/tutorial/extra/regex/

    Timeout


  • Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,325 Mod ✭✭✭✭Kingp35


    Yes I know how to read in lines of a text file its the breaking up into its pieces that im having problems with. That tutorial looks helpfull. Ill have a look through it.


  • Closed Accounts Posts: 884 ✭✭✭NutJob


    would Xml be usefull here (have code i can throw you)

    if not u could seperate using a tab or comma and use String liburary (indexof() .....)
    to parse it out (old school)


    By breaking it up what do u mean. Using the string liburary to parse it up as regular expressions can do that neatly.
    Or
    Converting integers and stuff back to ints


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    if the text is that simple, try using a string.indexof("[") type thing to find the read/write/commit/abort section. Then see if the remainder contains a "," symbol. If it does, you split it by the "," and parse the two integers out. Otherwise you know you only have 1 number, and you can just parse that out.

    Theres no need to use regex or xml.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Its pretty easy if you can read the file already.

    Something like.
    StringTokenizer st= new StringTokenizer(s,"[,]");
    

    Then read the first token which will always be a command. After that use that token to determine how to read the remainder but you can still use the stringtokenizer to pull the items from it.

    Something like.
    String command = st.nextToken();
    
    if (command.equalsIgnoreCase("write") {
        String arg1 = st.nextToken();
        String arg2 = st.nextToken();
    }
    else if (command.equalsIgnoreCase("commit") { 
        String arg1 = st.nextToken();
    }
    

    Of course this assumes you are getting in fixed formatting arguments.


  • Moderators, Society & Culture Moderators, Sports Moderators Posts: 12,325 Mod ✭✭✭✭Kingp35


    Thanks guys, got it working.


Advertisement