![](/img/trans.png)
[英]How do i convert an Object of mongodb to an String or integer in Java?
[英]how do I convert text to integer in Java
我想弄清楚如何在 Eclipse 中将文本字段中的文本转换为整数。 这是我到目前为止所拥有的,自从我从事这个程序以来已经有几天了,而且我也是 Java 编码的新手。 如果我已经在这里有了我需要的代码,我提前为问这个问题道歉。
protected void do_enterButton_actionPerformed(ActionEvent arg0) {
int scr1, scr2, scr3 = -1;
TestScores score = (TestScores)studentList.getSelectedValue();
int score1 = Integer.parseInt(score1TextField.getText());
score.setScore1(score1);
int score2 = Integer.parseInt(score2TextField.getText());
score.setScore2(score2);
int score3 = Integer.parseInt(score3TextField.getText());
score.setScore3(score3);
if (score1TextField != null) {
// this is where I need to convert to text to integer
}
score1
是您已经完成转换的地方。 Integer.parseInt(score1TextField.getText());
是正确的。
您在那里的 Integer.parseInt() 将文本(字符串值或字符序列)转换为整数。
不过,您应该将它们封装在 try-catch 块中。
try{
test = Integer.parseInt(textval);
}catch(NumberFormatException e){
System.out.println("A number was expected but not found.");
test = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.