Search code examples
javafilereaderaccountletter

Java make file reader account for specific letters


Well I'm almost finished with my world editor thanks to this great community, the only thing I need to know is how I can tell my read File code to process specific letters. When I hit enter on my keyboard I will write coordinates of a Vector3f to a text file, this Vector3f is the posistion of my active GameObject. My ProcessText method can read a text file and process the coordinates however he can only read ony type of format:

public void ProcessText()
     {      
        String file_name = "C:/Users/Server/Desktop/textText.txt";

        try
        {           
            ProcessCoords file = new ProcessCoords(file_name);
            String[] aryLines = file.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++)
            {
                System.out.println(aryLines[i]);    

                 if(aryLines[i].startsWith("makeGrass:")) {
                        String Arguments = aryLines[i].substring(aryLines[i].indexOf(":")+1, aryLines[i].length());
                        String[] ArgArray = Arguments.split(",");

                        this.makeGrass(Double.parseDouble(ArgArray[0]), 
                                       Double.parseDouble(ArgArray[1]), 
                                       Double.parseDouble(ArgArray[2]));                    
                }
            }   
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
     }

In the above example my ProcessText method can only process the coordinates if they are written like this:

makeGrass:x,y,z             //for example makeGrass:5,1,9

But when I press enter and write the coordinates from what me my engine gives I'm getting a different format:

makeGrass:(x y z)           //for example makeGrass:(3 1 4)

Now what I need to know is how I have to rewrite the code in my ProcessText method so it accounts for the other format that has brackets at the beginning and end and also with spaces to sepearta x from y and y from z instead of commas.

I really don't knwo where else I would find an answer to this question so I'd apreciate any help and explanation as to how this works.

Thanks a lot in advance!


Solution

  • You want to accept as many formats as possible?

    Instead of splitting I would try to match, this is safer and doesn't need any pre- or post-processing of the input or the received substrings:

    Pattern pattern = Pattern.compile("([0-9]+)"); // outside of method
    
    long[] ArgArray = new long[3];
    Matcher matcher = pattern.matcher(Arguments);
    int i = 0;
    while (matcher.find() && i < 3) {
       ArgArray[i++] = Long.parseLong(matcher.group(1));
    }
    // there was a mistake if i < 3, otherwise you have 3 numbers in ArgArray
    

    If you want to split, you could maybe try this: split("[^0-9]+")

    To only match makeGrass:(x y z)

    Pattern pattern = Pattern.compile("^makeGrass:\\(([0-9]+) ([0-9]+) ([0-9]+)\\)$");
    

    Like this you can directly match the line and have the 3 numbers in groups 1 - 3 (as String) after calling find once, if (matcher.find()) will decide if it's a valid makeGrass line and if so it can be processed in the if.