The Student Room Group

Won't compile?


import java.io.PrintStream;
import java.util.Formatter;

class HalfDollars {

public static void main(String[] arguments) {
int[] denver = { 2500000, 2900000, 3500000 };
int[] philadelphia = new int[denver.length];
int[] total = new int[denver.length];
int average;

philadelphia[0] = 2500000;
philadelphia[1] = 2900000;
philadelphia[2] = 3800000;

total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2]) / 3;

System.out.print("2003 production: ");
System.out.format("%,d%n", total[0]);
System.out.print("2004 production: ");
System.out.format("%,d%n", total[1]);
System.out.print("2005 production: ");
System.out.format("%,d%n", total[2]);
System.out.print("Average production: ");
System.out.format("%,d%n", average);
}
}


I get the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method format(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
The method format(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
The method format(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
The method format(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)

I'm using the eclipse IDE. Thanks.
Reply 1
What exactly are you trying to do that would be different from printing out the numbers in a println statement?
Reply 2
Looks like format is expecting an array of Objects, and since "int" is a primitive type, it doesn't count as an object. If you make it into an "Integer" object, it should work.

Or maybe it's expecting an array, even if it only has one element in it. Not sure if Java has short hand syntax for defining an array.

It's probably one of those things, or both. I'm not completely sure, but they'd be my guesses.
Reply 3
An int should be autoboxed to an Integer in JDK 1.5+.

JDK 1.5 is when that format method was introduced.

What JDK are you using? If it is higher than 1.5, then I think that code should work.

Here is an example: http://mindprod.com/jgloss/printf.html.

Like WineBlood mentioned, if you are having trouble getting format or printf to work, just use a normal println statement:


System.out.println("2003 production: " + total[0]);
I'd go with just using println myself. However, Baron's not quite right - this won't compile even in 1.5+. Autoboxing isn't applicable in this scenario - format() isn't just expecting an Object (hence needing an Integer rather than an int), it's expecting an Object[]. You need an array of length one. I'm not sure whether it will accept an int[] rather than an Integer[] - try both.

System.out.format("%,d%n", new int[] {total[0]});
System.out.format("%,d%n", new Integer[] {total[0]}); // This is where autoboxing comes in - saves you having to write "new Integer[] {new Integer(total[0])}".
Reply 5
Ok, I went to the trouble of compiling the code in the original post with JDK 1.6 and it works fine.

The two imports are unused, so you can delete them, but the code compiles fine.

I suggest you check you have JDK 1.5+.
Reply 6
Baron
Ok, I went to the trouble of compiling the code in the original post with JDK 1.6 and it works fine.

The two imports are unused, so you can delete them, but the code compiles fine.

I suggest you check you have JDK 1.5+.


Thanks guys, I think i have 1.5 installed..

btw what is autoboxing?
Reply 7
DOJO
Thanks guys, I think i have 1.5 installed..

btw what is autoboxing?


I'm not sure what to suggest, then :frown: I guess just having it installed isn't enough; check that eclipse is using 1.5 from you project properties. It must be something to do with your developement environment, rather than the code itself because it compiles and runs fine here.

In Java you have primitive types: int, long, float, double, char, boolean
You also have Classes that wrap around these primitives to provide extra functionality: Integer, Long, Float, Double, Character, Boolean.

From JDK 1.5 onwards, autoboxing was introduced which automatically converts between the primitive types and the wrapper Classes. For example in you had a method which took an int as a parameter, you could choose to give it either a variable or type int, or Integer. With previous JDKs, you would have to convert an Integer to an int before you could use the method.
More pertinently, many constructors and methods take an Object. Because primitive types aren't classes, they don't derive from Object, so you couldn't pass an int into myMethod(Object obj). A typical example would be an ArrayList or a HashMap: pre-1.5, these always took Objects, so you couldn't use a primitive type as an element in a list, or a key or value in a map. The wrapper classes exist primarily to give you something that stores a primitive value, but derives from Object. So you can put your int into an Integer object, and use it in something that needs an Object, like an ArrayList.

Before Java 1.5, you had to do this "boxing" manually by saying "myList.add(new Integer(myInt));" - ditto the "unboxing", where you'd have to get the Integer and then call intValue() to get the int out of it. Now, Java will do "auto-boxing" (and unboxing) - you can just use ints where you're supposed to use Integers (ditto for other primitive types), and Java will do the conversion behind the scenes. Do bear in mind, however, that just because you're not writing the boxing code doesn't mean it's not happening, so be aware of it for performance-critical stuff.

I'm now as confused as Baron as to why your code isn't compiling. Looking more closely, I note that PrintStream.format() is new in Java 1.5, so you MUST be using 1.5+ or the compiler would be complaining that it couldn't find format() instead. But also, I note that format() doesn't take an Object[] - it uses varargs, to take an arbitrary number of Objects. So I've no idea why the compiler is talking about "The method format(String, Object[]) in the type PrintStream" - it doesn't exist. I suggest just using println.
i'm also getting a similar problem when attempting to compile-The method println(java.lang.String) in the type java.io.PrintStream is not applicable for the arguments (java.lang.String, java.lang.String)

Here is the code-public class Peoplepeculiars {
public static void main(String[] args) {
short iq = 110;
double height = 5.5;
String haircolor = "blond";
String personality = "cynical";
char favouriteletter = 'B';
byte age = 18;
String name = "James";
String ethnicity = "White";
System.out.println(name + " is " + height +" feet tall, has an approximate iq of " + iq + ", a personality that could be best described as " + personality + " an age of " + age + " years old", "the favourite letter " + favouriteletter + ", and is " + ethnicity + "!" + "How interesting. If only someone really cared.");
}
}
Reply 10
You're passing two strings to println instead of one (as the error says).

println(... " years old", "the favourite letter " ...)

Quick Reply

Latest

Trending

Trending