user3322858
user3322858

Reputation: 43

Eclipse complains about following String.format usage

The program consists of the two following classes and should print time.
Within the class Melons Eclipse gives me an error under the method String.format and I do not understand why

public class Melons {

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

    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<24) ? s : 0);
    }

    public String toString()
    {
        // Problem here
        return String.format("%d:%02d:%02d %s", 
                             ((hour==0||hour==12) ? 12 : hour%12), 
                             minute,
                             second,
                             (hour<12 ? "AM" : "PM"));          
    } 
 }

class Apples
{
    public static void main(String args[])
    {   
        Melons melonsObject = new Melons();
        System.out.println(melonsObject.toString());
        melonsObject.setTime(13, 35, 9);
        System.out.println(melonsObject.toString());
    }   
}

Upvotes: 1

Views: 685

Answers (1)

Reimeus
Reimeus

Reputation: 159784

format has been a member method of String since Java 1.5. Ensure you are using this version or greater and that you don't have another String class on your classpath.

Upvotes: 2

Related Questions