PDA

View Full Version : Bufferreader help


Blacknight962
03-06-2009, 07:41 AM
What im trying to do:
Read a file called "script.txt" if the line starts with Walk then get the coordinates inbetween

Example: First line = Walk(500,500)
then the coordinates that it needs to get are 500,500

and then finally send it to Walk();

however im getting this error: http://img16.imageshack.us/my.php?image=errorqjk.jpg



BufferReader code:

try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Script.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println ("Full Line: " + strLine);

if (strLine.toLowerCase().startsWith("walk") ) {
int x = Integer.parseInt(strLine.substring(strLine.indexOf('(') + 1, strLine.indexOf(',')));
int y = Integer.parseInt(strLine.substring(strLine.indexOf(',') + 1, strLine.indexOf(')')));
Walk(x + "," + y);
}

}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}


Walk();


public void Walk(int x, int y)
{
menuID[0] = 920;
menuActionX[0] = x - areaX;
menuActionY[0] = y - areaY;
menuClick(0);
}



script.txt:


Walk(500,500)



Thanks,

Blacknight

Gox
03-07-2009, 06:57 AM
One of the nice things about java is that the error messages are generally pretty helpful in finding the error provided you know how to read them.

Looking at your compilation error message we can decipher a few things from it. I'll break the error message down so that it might help you fix your errors easier in the future.
client.java:348 (or is it 3481? hard to read in your screenshot)
This portion of the error message is stating that the error occurs on line 348 of your client.java file. This should help us pin down the offending line of code.

However, if we look closely at the error message we notice that the message also indicates the offending line of code in this portion of the error message.
Walk(x + "," + y) ;
Now we can be confident that we know which line of code is causing the error, we just need to know exactly why this line of code is incorrect.

Again, there are hints in the error message that should help us.
Walk(int, int) in ... cannot be applied to (java.lang.String)
What this is trying to tell us is that the signature for the method "Walk" takes two arguments both of which are of type "int", but our offending line of code is calling the Walk method and passing a single String argument rather than two ints.

Using all the above information we can be pretty sure this line of code is the issue, in the BufferedReader code that you posted.
Walk(x + "," + y);
The x + "," + y part of the line of code is acutally telling java to concatenate x and "," and y into a String and pass it as a single argument to the method Walk.

Now that we know exactly why java is complaining it should be easy to fix the error. We know that x and y are of type int, so we just need to pass them to the method Walk like so.
Walk(x , y);
Provided you parsed your script.txt file correctly x and y should equal 500, and your code should work as expected. If your parsing wasn't correct, you code should at least compile at this point.

A long winded explanation for a simple fix I know, but I tried to offer some insight on how to figure out what the compilation error is telling us so that in the future you'll be able to fix your errors easier.

Blacknight962
03-07-2009, 10:48 PM
Thanks, I was successfully able to compile it.

Blacknight962
03-08-2009, 09:17 AM
Now I have a new problem. While it's reading the lines & walking it completely ignores everything else until it is done. Such as mouse clicks and key presses.

under


Walk(x , y);


I've added:

while (x != sectionX + areaX && y != sectionY + areaY) {

}

sectionX + areaX = the X coord of the player
sectionY + areaY = the Y coord of the player

How can I have it not ignore everything else, while this is going on? (if possible?)

Thanks

riwan
03-09-2009, 05:03 AM
You need to implement threading to achieve that

Blacknight962
03-09-2009, 09:30 AM
You need to implement threading to achieve that

and that is done by using the keyword 'new' right?

riwan
03-09-2009, 10:08 AM
yes, using the keyword new and after that start()

Blacknight962
03-09-2009, 09:48 PM
solved

riwan
03-10-2009, 05:23 PM
You haven't implemented threading, google for threading examples.
- Class should extends Thread / implement Runnable
- I do really mean start() (not other function), so it should be br.start()
when you do start() it will go to function run() where you should define your code