Java 尝试将JTextField转换为int时出现NumberFormatException
在代码的这一部分中,我创建了一个类Java 尝试将JTextField转换为int时出现NumberFormatException,java,swing,Java,Swing,在代码的这一部分中,我创建了一个类Dizionario的对象并将其写入一个文件,首先调用construcor,接受3个参数(Path、String、int)。 我从3个JTextField中得到这3个参数,特别是最后一个(JTextField3)创建了这个错误,转换为int 这就是错误: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "javax.swing.JT
Dizionario
的对象并将其写入一个文件,首先调用construcor,接受3个参数(Path、String、int)。
我从3个JTextField中得到这3个参数,特别是最后一个(JTextField3)创建了这个错误,转换为int
这就是错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "javax.swing.JTextField[,62,11,302x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@9577f8b,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.<init>(Integer.java:677)
然后将i
作为参数(或直接调用新的整数(…)作为参数)
这是我的方法
private void CreateMouseClicked(java.awt.event.MouseEvent evt) {
Dizionario dic = new Dizionario(
(Paths.get(jTextField2.toString())),
jTextField1.toString(),
Integer.parseInt(jTextField3.toString()));
dic.writeToFile();
}
嗯,它不是
jTextField3.toString()
,而是jTextField3.getText()
。这是一个很大的区别,要查看toString()
返回的内容,请查看错误消息。您正在尝试分析以下内容:
"javax.swing.JTextField[,62,11,302x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@9577f8b,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]"
变成一个数字。嗯,它不是
jTextField3.toString()
,而是jTextField3.getText()
。这是一个很大的区别,要查看toString()
返回的内容,请查看错误消息。您正在尝试分析以下内容:
"javax.swing.JTextField[,62,11,302x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@9577f8b,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]"
输入一个数字。不要使用JTextField#toString
,使用JTextField#getText
返回文本字段的文本内容,例如
int i = new Integer(jTextField3.getText());
toString
通常用于提供有关对象的有用诊断信息
不要使用JTextField#toString
,使用JTextField#getText
返回文本字段的文本内容,例如
int i = new Integer(jTextField3.getText());
toString
通常用于提供有关对象的有用诊断信息
,但为什么第一个和第二个参数中没有错误?@maxpesa为什么要这样做getText
和toString
都返回String
,就编译器而言,它们是方法的正确选项…编译器无法猜测调用的结果可能是什么…@maxpesa:因为它们是字符串。您应该测试println语句中的字符串,以便自己查看。但是为什么第一个和第二个参数中没有错误?@maxpesa为什么要这样做getText
和toString
都返回String
,就编译器而言,它们是方法的正确选项…编译器无法猜测调用的结果可能是什么…@maxpesa:因为它们是字符串。您应该测试println语句中的字符串,以便自己查看。“尝试将JTextField转换为int时的NumberFormatException”betterApproach=JSpinner
SpinnerNumberModel“尝试将JTextField转换为int时的NumberFormatException”betterApproach=JSpinner
+SpinnerNumberModel
。