0

Today I wrote a simple program in eclipse Kepler in java 8. Actually, I copied it from some video tutorial. In that tutorial, it ran, but in my computer it didn't. Error line is

 String.format("%02d:%02d:%02d",hour,minute,second);

I don't understand what the error is here. It highlights the method format(String,object[]) in the type String are not applicable for the argument(String, int, int, int)

public class Demo {

private int hour;
    private int second;
    private int minute;

    public void setTime(int h,int m,int s){
        hour=((h>=0 && h<24)?h:0);
        minute=((m>=0 && m<60)?m:0);
        second=((s>=0 && s<60)?s:0);
    }

    public String railwayTime(){
        return String.format("%02d:%02d:%02d",hour,minute,second);//error in this line
    }

    public String regular(){
        return String.format("%02d:%02d:%02d %s",((hour==0 ||hour==24)?12:(hour%12)), minute, second, (hour>=12)?"AM":"PM");//error in this line
    }
}

public class ShowTime {
    public static void main(String[] args){
        Demo d=new Demo();
        System.out.println(d.railwayTime());
        System.out.println(d.regular());
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
rajeev ranjan
  • 31
  • 1
  • 1
  • 5
  • As the exception states, you have to provide the formatting arguments as an array, not as individual arguments. – Vadim Landa May 25 '15 at 14:00
  • 1
    Using Java 8 here. I can't reproduce this error. Do you think you could show us some more code? And are you sure you're using Java 8 too? – PakkuDon May 25 '15 at 14:07
  • 1
    `String.format` is introduced in Java 5 and since then it accepts `Object...`, I just don't understand how did ypu misused the standard library to get this result. – Dmitry Ginzburg May 25 '15 at 14:43
  • 1
    Read this: [What is "compiler compliance level" in Eclipse?](http://stackoverflow.com/q/22584427), set your level to java 8 and then be happy. – Tom May 25 '15 at 14:46

3 Answers3

6

The exception asks you for an array instead comma-sepparated strings:

// incorrect
String.format("%02d:%02d:%02d",hour,minute,second);

// fast but correct
Object[] data = { hour, minute, second };
String.format("%02d:%02d:%02d", data);

But actually, method format(String,object[]) does not exists in String, it is: format(String pattern, Object... arguments) what should work with commas ,. There is something with your syntax, but not in the shown code.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    `String data` -> `String[] data` – Dmitry Ginzburg May 25 '15 at 14:39
  • 1
    Also, surely `hour`, `minute` and `second` are `int`s, so this wouldn't compile. You should create `data` as `Object[] data = ...` – Dmitry Ginzburg May 25 '15 at 14:40
  • @rajeevranjan What was the previous setting for compliance level, such that it gave the error you saw? – Stuart Marks May 25 '15 at 16:29
  • @StuartMarks Most likely Java 1.4. In some IDE this is the standard level if you don't define something else. – Tom May 25 '15 at 16:50
  • @Tom Yes it must be 1.4 if varargs wasn't supported. But could the default for Eclipse Kepler (released 2013) really be 1.4 (released 2002)? – Stuart Marks May 25 '15 at 17:24
  • @StuartMarks Sadly it is. I'm not 100% sure if this is a problem with Eclipse itself or the installed JDK, but I experienced that myself during my time with Eclipse. I'm using IntelliJ and it currently uses Java 6 as the default (with JDK 8 installed). Kind of sucks :D. – Tom May 25 '15 at 17:27
1

A real answer to this problem is only your type int. You don't have to use Object specifically but you have to use a type that inherit from Object and int is a raw type that does not inherit from Object, like all raw types. So you can use Integer instead of int to solve your problem.

Mathan
  • 7
  • 5
0

I know this is old, but I have a guess.

private int hour;
private int second;
private int minute;

As above, you declared hour, second and minute as of int type which is a primitive data type and is not compatible to the Object type.

You might wanna change them to:

private Integer hour;
private Integer second;
private Integer minute;

Integer is a wrapper for the primitive type int and is used to objectify it. By then, the line String.format("%02d:%02d:%02d",hour,minute,second); should work fine.