How to use printf in the new java 7

0 Miguel Castillo · June 19, 2014
Im trying to use printf but i get this error and dont understand what it means can you also explain why that happens.package bank;
import java.util.Random;

public class Money implements Runnable {
String name;
int time;
Random r = new Random();

public Money(String s){
s = name;
time = r.nextInt(999);

}

public void run(){
System.out.printf("%s is sleeping for %d", name, time);

}

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Miguel Castillo · June 19, 2014
This is the error i get:


Exception in thread "Thread-0" Exception in thread "Thread-1" Exception in thread "Thread-2" java.lang.Error: Unresolved compilation problems: 
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, int)
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)


at bucky.Apple.run(Apple.java:18)
at java.lang.Thread.run(Unknown Source)
java.lang.Error: Unresolved compilation problems: 
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, int)
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)


at bucky.Apple.run(Apple.java:18)
at java.lang.Thread.run(Unknown Source)
java.lang.Error: Unresolved compilation problems: 
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, int)
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)


at bucky.Apple.run(Apple.java:18)
at java.lang.Thread.run(Unknown Source)
-2 Miguel Castillo · June 20, 2014
thank you to everyone
0 Rian F · February 24, 2015
Just like Mathias said, name must be set to s. There are two ways to do it. The first is to say "name = s;". The second is to use a call to this as shown in the example below

import java.util.*;
class Money implements Runnable
{
private String name;
private int time;
private Random r = new Random();

public Money(String s)
{
this.name = s;
time = r.nextInt(999);
}
public void run()
{
System.out.printf("%s is sleeping for %d", name, time);
}
}
public class Main
{
public static void main(String [] args)
{
Money m = new Money("Hello");
m.run();
}
}
0 Branislav Lazic · February 24, 2015
Exception is self explanatory. 


The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, int)


Which means it should be like this:


System.out.printf("%s is sleeping for %d", new Object [] {name, time});

However, preferred way would be:


System.out.printf(String.format("%s is sleeping for %d", name, time));

since signature of "format" method is:

 format(String format, Object... args)
  • 1

Java / Android Development

124,707 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator