我目前正在研究从网络获取数据并将其打印在 JTextArea 上的代码。在这两者之间,我试图根据小数位对齐数字。这是在实现十进制对齐之前工作的代码:
private static final String NewLine = System.getProperty("line.separator");
String NetString = "";
byte[] data = p.getData();
NewString += "SID: " + BuildShort(data,4) + NewLine;
NewString += "DID: " + BuildShort(data,6) + NewLine;
这是新的
NewString += String.format("%-8s%11.5f" + NewLine, "SID : ", BuildShort(data,4));
NewString += String.format("%-8s%11.5f" + NewLine, "DID : ", BuildShort(data,6));
我收到了错误消息
Exception in thread "Thread-2" java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at MT302.ParsePacket(MT302.java:97)
at MK20_DataView.run(MK20_DataView.java:261)
at java.lang.Thread.run(Unknown Source)
你知道我为什么会收到这个错误吗?