如何在 java 8 中使用 String.format?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30440073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:38:28  来源:igfitidea点击:

How to use String.format in java 8?

javastringjava-8

提问by rajeev ranjan

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

今天我用java 8用eclipse Kepler写了一个简单的程序,其实是从某个视频教程里复制过来的。在那个教程中,它运行了,但在我的电脑中却没有。错误行是

 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 Stringare not applicable for the argument(String, int, int, int)

我不明白这里的错误是什么。它突出显示了format(String,object[])类型String中的方法不适用于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());
    }
}

回答by Jordi Castilla

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.

但实际上,方法format(String,object[])不存在于String,它是:format(String pattern, Object... arguments)什么应该与逗号一起使用,。您的语法有问题,但在显示的代码中没有。

回答by Mathan

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.

这个问题的真正答案只是你的类型 int。您不必专门使用 Object,但必须使用从 Object 继承的类型,而 int 是不从 Object 继承的原始类型,就像所有原始类型一样。所以你可以使用 Integer 而不是 int 来解决你的问题。

回答by Someone

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

我知道这是旧的,但我有一个猜测。

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

As above, you declared hour, secondand minuteas of int type which is a primitive data type and is not compatible to the Objecttype.

如上所述,您声明hour,secondminuteas 属于 int 类型,它是一种原始数据类型并且与该Object类型不兼容。

You might wanna change them to:

您可能想将它们更改为:

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

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

Integer是原始类型的包装器,int用于对象化它。到那时,该线路String.format("%02d:%02d:%02d",hour,minute,second);应该可以正常工作。