1

Well I have a HUGE long list of packet sizes that need to be re-written, and I don't want to do it by hand, so I'm going to make a program for it.

public static OutcommingPacket aClass198_1993 = new OutcommingPacket(68, 8);

That's and example of one of the lines, what I need to do, is get the program to go to the 68, store it in the packet string, and the get the number 8, and store that in the size string.

Here is my code so far.

public class PacketSizeFixer {

public static final String IN = "./out/OldPacketSizes.txt";
public static final String OUT = "./out/PacketSizesFormatted.txt";

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(IN));
    BufferedReader writer = new BufferedReader(new FileReader(OUT));

    String line;
    String packet, size;
    while ((line = reader.readLine()) != null) {
        packet = line.substring(line.indexOf("new OutcommingPacket(", line.indexOf(", ")));
        size = line.substring(line.indexOf(", "), line.indexOf(");"));
    }
}

}

I'm not sure if im doing it the right way, because I keep getting a String index out of range

please help!

By the way, not all packets have the same name, some are longer, some are shorter, and the packets could be double digits, and so can the sizes. Please help!

2
  • Please post the actual exception and stack trace as part of your question. This is one of those localized debugging questions that is probably more immediately solvable if people can see the problem. Commented Jul 12, 2012 at 0:30
  • 1
    'Outcoming' is spelled with one 'm'. You are using a Reader for your writer. You ignore the -1 return from indexOf if the substring isn't found. You need to add the length of the string you're searching for to the starting index of the substring call. Commented Jul 12, 2012 at 1:45

3 Answers 3

3

I'm assuming a lot here...

while ((line = reader.readLine()) != null) {
    String pair = line.substring(line.lastIndexOf("("), line.lastIndexOf(")"));
    String values[] = pair.split(","); //values[0] == packet, values[1] == size
}
Sign up to request clarification or add additional context in comments.

Comments

3

You probably get this error cause a substring that you're looking for is not found (returns -1) and then you call substring without checking the returned index.
Try:

int index1 = line.indexOf("new OutcommingPacket(");
int index2 = line.indexOf(", ");
if (index1 > -1 && index2 > index1)
   packet = line.substring(index1, index2 - index1);
//same for the rest

1 Comment

add: System.out.println("index1 = "+index1+"; and index2 = "+index2); just to make sure...
2

From your example, it sounds like the information you want to extract is:

#,#

So why don't you just use a regular expression ?

CharSequence inputStr = "new OutcommingPacket(68, 8)";

String patternStr = "(\\d+),(\\d+)";

// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();

if (matchFound) {
    // Get all groups for this match
    for (int i=0; i<=matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
    }
}

NOTE: I haven't tested this exact Pattern, but it should be close to correct at least...

1 Comment

I think that because the OP is parsing what appears to be Java code that the input will encounter problems with a formatter wrapping portions of the code onto the next line. I also think the pattern string will need to make sure it only finds numbers located in the specified constructor pattern as well as handle possible white space where Java would allow white space. Of course, that last bit doesn't fit with the OP's approach at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.