[英]How can I convert input from JTextField into double. Input value is from user
I'm trying to use an input from JTextField and turn it into double 'coz I'm gonna use the input in a mathematical formula in my program.. but when I try to run the codes, it gives an error.. I think I got the wrong way of parsing.. ANY HELP? 我正在尝试使用JTextField的输入并将其转换为双行,因为我要在程序中的数学公式中使用该输入..但是当我尝试运行代码时,它给出了一个错误..我认为我解析的方法有误。任何帮助吗? Thanks!
谢谢!
xLabel = new JLabel("Subject");
xTF = new JTextField(4);
xTF.addKeyListener(new KeyHandler());
xString = xTF.getText();
xDouble = Double.parseDouble(xString);
KeyListener
on text components, use a DocumentFilter
, take a look at Implementing a Document Filter and DocumentFilter Examples for more details KeyListener
,使用DocumentFilter
,更多信息请看实现文档过滤器和DocumentFilter示例 InputVerifier
instead. InputVerifier
。 See Validating Input for more details JFormmattedField
or JSpinner
instead. JFormmattedField
或JSpinner
。 See How to Use Formatted Text Fields and How to Use Spinners for more details It's difficult to tell, but remember, you're operating in an event driven environment, until the user does something, you should not be trying to get the value of a field 很难说,但是请记住,您是在事件驱动的环境中进行操作的,除非用户执行了某些操作,否则您不应该尝试获取字段的值
Remove any whitespace available by using trim() on the string 通过在字符串上使用trim()删除任何可用的空格
xDouble = Double.parseDouble(xString.trim());
Where i'm assuming xString is a valid String and xDouble is of type Double 我假设xString是有效字符串,而xDouble是Double类型
You should use parseDouble(String s)
and exception which is related is NumberFormatException
您应该使用
parseDouble(String s)
和相关的异常是NumberFormatException
The java.lang.Double.parseDouble() method returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double.
java.lang.Double.parseDouble()方法返回一个新的double,该double初始化为指定的String表示的值,如Double类的valueOf方法所做的那样。
For example: 例如:
public class Main {
public static void main(String[] args) {
String str = "50.00001";
double retval =Double.parseDouble(str);
System.out.println("Value = " + retval);
}
}
That looks fine, did you type in the right values in your textfield before you tested it? 看起来不错,在测试之前是否在文本字段中输入了正确的值? Per example if you leave it empty or type in "1,3" it wouldn't probably accept it.
例如,如果您将其保留为空或输入“ 1,3”,则可能不会接受。
Edited: I just did a test and as I said, it only accepts values like this: "1.3" 编辑:我刚刚做了一个测试,正如我所说,它只接受这样的值:“ 1.3”
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyApp extends JFrame implements ActionListener {
JTextField textField;
JLabel label;
JButton okButton;
public MyApp() {
textField = new JTextField(4);
label = new JLabel("N/A");
okButton = new JButton("Ok");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
add(textField, BorderLayout.NORTH);
add(label, BorderLayout.CENTER);
add(okButton, BorderLayout.SOUTH);
okButton.addActionListener(this);
pack();
}
public static void main(String[] args) {
MyApp app = new MyApp();
app.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == okButton) {
double d = 0;
d = Double.parseDouble(textField.getText());
label.setText(String.valueOf(d));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.