1

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());
    }
}
4
  • As the exception states, you have to provide the formatting arguments as an array, not as individual arguments. Commented May 25, 2015 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? Commented May 25, 2015 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. Commented May 25, 2015 at 14:43
  • 1
    Read this: What is "compiler compliance level" in Eclipse?, set your level to java 8 and then be happy. Commented May 25, 2015 at 14:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

String data -> String[] data
Also, surely hour, minute and second are ints, so this wouldn't compile. You should create data as Object[] data = ...
@rajeevranjan What was the previous setting for compliance level, such that it gave the error you saw?
@StuartMarks Most likely Java 1.4. In some IDE this is the standard level if you don't define something else.
@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)?
|
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.