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

Java Text Parsing Question

Options
  • 24-09-2004 10:26am
    #1
    Registered Users Posts: 2,102 ✭✭✭


    Hi all!

    I'm hitting a brick wall here. Trying to relearn Java and having a problem with code in work! EEP.

    Basically the program reads in a text file, changes the format of the text and outputs it.

    So far I can read in the file, scan through it wrong and output it, errors and all.

    My problem is with the algorithm which should.

    Scan for something that looks like ;B[ab] or ;W[bc] or whatever, the ';B' or ':W' is for 'Black' or 'White' in a game of go. The 'ab' is it's co-ordinate on the board. These would need to be changed to:

    stone1=B&stone1=[a2]
    stone2=W&stone2=[b3] ie the second letter of the coordinate needs to be numerical and the format needs to change.

    I KNOW this is easy, and I'm not looking for the actual code, just a prod in the right direction.

    I have something like:

    while((line = bfReader.readLine()) != null)
    { while(SOMETHING HERE THAT I CANT FIGURE)
    {
    get substring
    get co-ordinates
    change them up
    stick them into some variable
    write the variable at the end.
    }
    }

    I know some of these steps are wrong. I had the inner while loop as:
    while((pos = line.indexOf(";B", tempPos + 1)) > -1 || (pos = line.indexOf(";W", tempPos + 1)) > -1)
    But that needs ':B' to occur first.

    Any help is deeply appreciated.

    Thanks for your time, hope it made sense!

    Ross


Comments

  • Closed Accounts Posts: 47 PhilH


    Well, here's some suggestions...

    Look at the javadocs for the method split on java.lang.String. You should be able to use that to split each line of the file into an array of single stone descriptions like B[af].

    The do some simple checking that the second character is a [ and the fifth character is a ].

    Then the stone colour is the first character. Your first coordinate is the third character. Your second coordinate is based on the fourth character.

    To transform the letter into a number, you could use the numeric value of the 'char', or you could use String's indexOf to get a position in a string of expected values.

    Like you say, shouldn't be too hard.

    PHiL


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


    Bonus points if you use StringBuffer instead. You can use String, but StringBuffer is more memory efficient.


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Any help is deeply appreciated.

    For tricky string matching you could use Regular Expressions. For example...
    import java.io.File;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    class test {
    	public static void main( String[] args ) {
    		byte baBuffer[] = new byte[4096];
    		try {
    			BufferedReader br = new BufferedReader( new FileReader( "test.txt" ) );
    			String sLine = null;
    			while( (sLine = br.readLine()) != null ) {
    				Pattern p = Pattern.compile( "^;([BW])\\[([a-z]*)\\]$" );
    				Matcher m = p.matcher( sLine );
    				if( m.matches() ) {
    					System.out.println( "Match" );
    					String sBW = m.group( 1 );
    					String sCoord = m.group( 2 );
    
    					System.out.println( "Found coordinate: " + ( sBW.equals( "B" ) ? "Black" : "White" ) + ": " + sCoord );
    				}
    			}
    		}
    		catch( IOException ignore ) {}
    	}
    }
    

    Which works for a text file of
    ;B[ab]
    ;W[bc]
    ;W[dd]
    
    

    .cg


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Holy crap man! I would never have thought of regular expressions. Gotta look up all that crazy stuff now and see what it does!

    It's time for some JavaDoc reading.

    Thanks a million for the (very fast) responses! :)

    Gonna get plugging away at this and try and get her up and running by the end of today! (I have lots of stuff to do as well, I'm hopefully not *that* slow) (I am)

    Thanks again,
    Ross


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Hrrmage, the code above doesn't do anything :o( It reads in fine but i just get "press any key to continue"

    I bettere get digging a bit deeper into javadoc. Sigh I seem to have some kind of mental block when it comes to programming. It's amazing how much you foget after a year or two!

    Thanks again!


  • Advertisement
  • Closed Accounts Posts: 47 PhilH


    cgarvey is right about being able to do fancy things with regular expressions, but you're not trying to do fancy things here, so that's probably too much complexity to introduce to solve a simple problem.

    If you want some compromise between that and the very simple approach, then look at using the MessageFormat class in the java.text package to parse a stone description into an object array (look at the parse method).

    Unless this is meant to be an exercise in doing the most obscure and involved thing you can come up with though, you should adhere to the maxim KISS - "keep it simple, stoopid". It's a simple problem, so someone should be able to understand the solution without a PhD.

    PHiL


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    It's for work, but I'm trying to relearn the Java language as I go on the way. I'm WAY out of practice! I'd rather keep it simple but am happy to learn about Reg Exps now!
    Ross


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Works! Used this:
    try
    {
    while((line = bfReader.readLine()) != null)
    {
    theSplitLine = line.split(";");

    for(int i = 0; i < theSplitLine.length; i++)
    {
    if(theSplitLine.length() == 5 && theSplitLine.charAt(1) == '.charAt(4) == ']')
    {
    stoneCount++;

    fileContents+= ("Stone" + stoneCount + "=" + theSplitLine.charAt(0) + "&" + "Stone"
    + stoneCount + "=" + "[" + theSplitLine[i].charAt(2) + (theSplitLine[i].charAt(3) - 'a' + 1) + "]" + "\n");
    }

    }

    }


    Going to learn the regex stuff still cos I know this thing is gonna get out of control!

    Thanks everyone!

    Ross


Advertisement