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

Best way to alter a Java String for output

Options
  • 17-11-2005 2:56am
    #1
    Registered Users Posts: 3,579 ✭✭✭


    My Java's a bit rusty, but I'm working on a program to take a text file in, and search through it and output some lines from it (but not all) to another text file.

    I have the Scanner and stuff set up, but then I got stuck when it came to altering a String for output to the output file.

    So far, it searches through the input file using this:
    FileReader reader = new FileReader(inputFileName);
    PrintWriter out  = new PrintWriter(outputFileName);
    Scanner in = new Scanner(reader);
    
    while (in.hasNextLine()) {
     String line = in.nextLine();
    
     if(line.equals(stringKeyword1))
      out.println("Keyword 1 was found!");
    

    Here's the bit I'm stuck on:
    Once it finds a line (line.equals(" ")) that is:
    <gml:coordinates>100,200 300,400</gml:coordinates>
    

    I want it to output this (instead):
    coordIndex [ 100,200 300,400 ]
    

    So, something like:
     if(line.equals(stringKeyword1))
      out.println("coordIndex [" + line.charAt[position whatever] + ... etc.);
    

    As I said, my Java is a bit rusty so I don't know if I should use a substr() method or a char array with a for loop (not that I remember how to do them anyway!

    Help aprreciated. :)


Comments

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


    Sounds like XSLT would be better suited for that.


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


    if you find a line starting with: <gml:coordinates>

    Create a substring of it which starts on the 17th character, lasts for (length of string - 17 - 18). That should give you just the parts you want, then print that as normal.

    Why you'd use XSLT to do this i don't know, it'd be overkill for this :P


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


    Because it can handle XML easily and format it.

    There are certainly a number of ways to do this. You could load it as DOM object and parse the object data. Wouldn't say its the fastest way (to code it) but certainly the proper way. It would handle any XML changes at a later date easier.

    Your option is correct, but I'd do one further and look for the first occurance of ">" in the line rather then go to a particular column. Just in case the line has been formatted in some way. You would probably have to find the occurance of "<" after that.

    That would be using indexof() and substring. You would have to parse the found string for XML control codes if you believe they may go in at a later date (eg.   , >, < ).


  • Closed Accounts Posts: 324 ✭✭madramor


    the correct way would be to use a SAX parser as it is simpler
    and faster than DOM

    the reason you would need to use a parser is,
    when you look at a xml file in a browser it is formatted
    by the browser, the actual file could all be on one line
    so by grabbing a line from the file could give you the entire file
    rather than the line you expect


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


    madramor wrote:
    the correct way would be to use a SAX parser as it is simpler
    and faster than DOM

    Yep your correct.


  • Advertisement
  • Registered Users Posts: 3,579 ✭✭✭BopNiblets


    It must be in Java, I forgot to mention that bit (D'oh).

    I've never heard of DOM, XSLT or SAX though, I don''t think I need to learn that kind of stuff to just make it output what I want...
    I know what a parser is, kind of.... but I believe my program will be a simplified parser once it does what I want it to do.
    I also should mention I took that Scanner code from an example in a book and just changed it around a bit for my purposes, it may have given ye a false premise of my non existant skillz! :p
    if you find a line starting with:
    Create a substring of it which starts on the 17th character, lasts for (length of string - 17 - 18). That should give you just the parts you want, then print that as normal.
    Thanks! This sounds like what I need, is it something like this (remember, Java = rusty!):
    string1 = line.substr( ? )
    


  • Registered Users Posts: 1,636 ✭✭✭henbane


    BopNiblets wrote:
    Thanks! This sounds like what I need, is it something like this (remember, Java = rusty!):
    string1 = line.substr( ? )
    
    substring


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    I'd go with StringTokenizer rather than a bunch of substring's and indexOf's.
    // set '<', '>', ' ' as the delimiting characters
    StringTokenizer tk = new StringTokenizer(line, "<> "); 
    tk.nextToken(); // discard the first "gml:coordinates" part
    System.out.println("coordIndex [ " + tk.nextToken() + " " +
     tk.nextToken() + " ]");
    


  • Registered Users Posts: 3,579 ✭✭✭BopNiblets


    Aha, much love guys, thanks very much. :D

    I can experiment with this stuff now, see what works best.

    kelsocoolio.jpg


Advertisement