Help with printf
Hi, folks! (Yes-I've already searched the forums and can't find an answer)
I am using Eclipse, and am trying to use printf, since I am told that it is a good habit to get into (by a professor, so it's not like I can arbitrarily use println). I have been trying to figure out why printf (and println, for that matter) are returning errors at these specific places when neither method has any problems anywhere else in my code:
privatevoid buildSchedule (){
System.out.printf("==> buildSchedule...\n" );
schedule.scheduleActionBeginning (1, this,"step");
}
I am getting this error:
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String)
I tried finding a tutorial online, but I don't seem to be able to find out what I'm doing wrong. Have I failed to import a necessary package or something?
Thanks for your help ;-)
Have you looked at the API?The printf methods (two of them) belong to the PrinterStream class and neither of them take a single String as a parameter.
You are saying that you are getting a compile error, not a runtime error, when you use 'println' in the place of the 'printf' code that you posted?If yes then what happens when you comment out the line that follows it?
I looked at the API already, and found that the PrinterStream class doesn't exist. I looked at the PrintStream class, and found no printf method defined in it.
> I looked at the API already, and found that the
> PrinterStream class doesn't exist. I looked at the
> PrintStream class, and found no printf method defined
> in it.
OOPS! A typo on my behalf. It is the PrintStream class. If you cannot find the printf method then you do not have Java 1.5.
> You are saying that you are getting a compile error,
> not a runtime error, when you use 'println' in the
> place of the 'printf' code that you posted?
>
> If yes then what happens when you comment out the
> line that follows it?
When I do this?
private void buildSchedule () {
System.out.printf( "==> buildSchedule...\n" );
//schedule.scheduleActionBeginning (1, this, "step");
}
There is no change; I get the same error message.Recall that I'm getting error messages for both printf and println; in both places in this snippet:
private void buildSchedule () {
System.out.printf( "==> buildSchedule...\n" );
schedule.scheduleActionBeginning (1, this, "step");
}
public void step () {
System.out.println( "==> Model step %.0f:\n", getTickCount() );// Checking step #
for ( int i = 0; i < playerList.size(); i++ ) {
Player aPlayer; // Makes new players when needed
aPlayer = (Player) playerList.get (i); // Identifies players
aPlayer.step ();
}
It is a compile error, and I don't know how I'm getting the concatenation in the parentheses wrong...
>If you cannot find the printf method then you> do not have Java 1.5.I found it--I was looking online at the wrong set of APIs, for 1.3 instead of 1.5...hang on for a sec...
Trying switching your printf and println statments around. That is switch the method names and leave the parameters the same.
> Trying switching your printf and println statments
> around. That is switch the method names and leave the
> parameters the same.
I did some of that--I think I understand the differences between printf and println now, but I'm still getting error messages on three usages:
System.out.printf( "-- moved to new x,y=%d,%d.\n", x, y );
}
else {
System.out.printf( "-- couldn't find a place to move! tries=%d\n",
tries);
}
The first printf tells me that it's not the right method for "String, int, int", the second error says it's not right for "String, int"
In this snippet, it says println isn't right to use for "String, double":
public void step () {
System.out.println( "==> Model step %.0f:\n", getTickCount() );// Checking step #
for ( int i = 0; i < playerList.size(); i++ ) {
Player aPlayer; // Makes new players when needed
aPlayer = (Player) playerList.get (i); // Identifies players
aPlayer.step ();
}
What's the right print statement to use for these two, then?
And also now I seem to have messed something up with this printf statement--something to do with the concatenation, I think:
System.out.printf( "- created player with ID=%d placed at"
x, y=%d,%d.\n, aplayerID, aPlayer.getX(), aPlayer.getY());
}
The error message is that x, y=%d,%d.\n cannot be resolved. Have I done something wrong here?
Did you read my reply #7?
Do you understand how parameters work?
If I have a method like this
public void method(int a, String b, int c) {}
Then these method calls will fail.
method(1, 2, "Hello");
method(1);
method("hello");
method("hello", 1, 2 );
etc
Why? because none of the actual parameters in the method calls match the formal parameters in the method header. So now you need to find a printf or println method that has formal parameters that match your actual parameters. I bet you can't find them, neither can the compiler and that is why you are getting the error messages.
Bottom line, your parameters must match!
Message was edited by:
flounder
> Bottom line, your parameters must match!
Here's the problem; I can't find a print method that will let me use the parameters (String, int, int) or (String, int) or (String, double). I've gone through every single one of the methods in PrintStream and PrintWriter; am I missing something? I am importing java.io.PrintStream and java.io.PrintWriter; I tried using print, printf, and println on each one--what am I missing?
> Here's the problem; I can't find a print method that
> will let me use the parameters (String, int, int) or
> (String, int) or (String, double).
That's my whole point. Those methods do not exist. You cannot pass the parameters you want. You need to work out exactly what you want to print and which methods you can call to achieve that. Note: you may have to make several calls to printf or println or print to get the desired result.
> > Here's the problem; I can't find a print method
> that
> > will let me use the parameters (String, int, int)
> or
> > (String, int) or (String, double).
>
> That's my whole point. Those methods do not exist.
> You cannot pass the parameters you want. You need to
> work out exactly what you want to print and which
> methods you can call to achieve that. Note: you may
> have to make several calls to printf or println or
> print to get the desired result.
Hmm. Can I then ask--how should I restructure this snippet to avoid those errors?I've looked through all the API, including the external jars that I'm using for this project, and I don't understand why I can't concatenate a string and an int in the same command to print. Am I just being dense, or is there something wrong with this statement? I don't understand why I can run this program just fine on my school computers, but when I bring it home, I'm getting these errors.
System.out.println( "- created surfer with ID=%d placed at x,y=%d,%d.\n", asurferID, aSurfer.getX(), aSurfer.getY() );
}
Shouldn't I be able to use a string, then add whatever I want in the way of variables, objects, whatever in order to have the proper information print out in my console? If not, am I supposed to use plus signs and not commas?
So what I did was this: I used plus signs instead of commas. It works, although I don't actually know why. At least the errors are gone, the program runs, and I get all the printed information now. Thanks
> Trying switching your printf and println statments
> around. That is switch the method names and leave the
> parameters the same.
I said the above in reply #7.
System.out.println( "- created surfer with ID=%d placed at x,y=%d,%d.\n", asurferID, aSurfer.getX(), aSurfer.getY() );
This should be a PRINTF method call not a PRINTLN call.
> So what I did was this: I used plus signs instead of
> commas. It works, although I don't actually know
> why. At least the errors are gone, the program runs,
> and I get all the printed information now. Thanks
println will not work with extra arguments.
There is no reason that printf() will not. It uses the dotted argument notation.
Using plus will not allow you to use the format characters that printf allows.