2012-09-14 8 views
6

スロー:String.Formatの()は、Java 1.6でこのコードは正常に動作しますFormatFlagsConversionMismatchException

java.util.FormatFlagsConversionMismatchException: Mismatched Convertor =s, Flags= # 
     at java.util.Formatter$Transformer.transformFromString(Formatter.java:1020) 
     at java.util.Formatter$Transformer.transform(Formatter.java:861) 
     at java.util.Formatter.format(Formatter.java:565) 
     at java.util.Formatter.format(Formatter.java:509) 
     at java.lang.String.format(String.java:1961) 

回避策:

public static String padLeft(String s, int n) 
{ 
    if (n <= 0) 
     return s; 
    int noOfSpaces = n * 2; 
    String output; 
    noOfSpaces = s.length() + noOfSpaces; 
    output = String.format("%1$#" + noOfSpaces + "s", s); 
    return output; 
} 

しかし、新しいバージョン(および他のいくつかのVMの実装が)このExceptionを投げますか?

+0

バージョン、およびどのJavaのバージョンには、上の失敗しない:ちょうどStringBuilderを使うのか?各バージョンで同じデータをテストしていますか(つまり、Javaバージョンは実際には無関係であり、実際に問題を引き起こす特定の入力ですか?) –

+0

はい。どちらの場合も同じデータ入力です。 Oracle JDKでは常に動作しますが、IBM jdkでは失敗します。他の人が同じ理由を報告しています - https://issues.apache.org/jira/browse/SSHD-104 – user170008

+0

jre-1.7.0_06を使用しています。 1.6.0_30で動作します – Achow

答えて

2

回避方法を尋ねました。それが動作しないJavaの

public static String padLeft(String s, int n) { 
    if (n <= 0) 
     return s; 
    int noOfSpaces = n * 2; 
    StringBuilder output = new StringBuilder(s.length() + noOfSpaces); 
    while (noOfSpaces > 0) { 
     output.append(" "); 
     noOfSpaces--; 
    } 
    output.append(s); 
    return output.toString(); 
} 
0

フォーマット文字列に#フラグを使用しているので、Formattableを引数(doc)に渡す必要があります。

回避策はありますか?

フォーマット文字列に#を使用しないでください。

関連する問題