Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据输入将文本输入转换为int或double_Java_Jtextfield - Fatal编程技术网

Java 根据输入将文本输入转换为int或double

Java 根据输入将文本输入转换为int或double,java,jtextfield,Java,Jtextfield,我有一个JFrame,我从文本字段中获取输入并将其转换为整数。 如果它是double,我还想将其转换为double,如果它不是int或double,可能会返回一条消息。我该怎么做 我当前的代码: int textToInt = Integer.parseInt(textField[0].getText()); 要保持精度,请立即解析为BigDecimal。 这当然不是特定于语言环境的 try { int textToInt = Integer.parseInt(textField[0

我有一个JFrame,我从文本字段中获取输入并将其转换为整数。 如果它是double,我还想将其转换为double,如果它不是int或double,可能会返回一条消息。我该怎么做

我当前的代码:

 int textToInt = Integer.parseInt(textField[0].getText());
要保持精度,请立即解析为BigDecimal。 这当然不是特定于语言环境的

try {
    int textToInt = Integer.parseInt(textField[0].getText());
} catch(NumberFormatException e) {
    try {
        double textToDouble = Double.parseDouble(textField[0].getText());
    } catch(NumberFormatException e2) {
        System.out.println("This isn't an int or a double";
    }
}
要保持精度,请立即解析为BigDecimal。
这当然不是特定于语言环境的。

检查字符串是否包含小数点

try {
    int textToInt = Integer.parseInt(textField[0].getText());
} catch(NumberFormatException e) {
    try {
        double textToDouble = Double.parseDouble(textField[0].getText());
    } catch(NumberFormatException e2) {
        System.out.println("This isn't an int or a double";
    }
}
if(textField[0].getText().contains(".")) 
    // convert to double 
else 
    // convert to integer 
没有例外


在执行上述操作之前,可以使用正则表达式检查字符串是否为数字。一种方法是使用模式
[0-9]+(\[0-9]){0,1}
。我不是正则表达式最好的,所以如果这是错误的,请纠正我

检查字符串是否包含小数点

if(textField[0].getText().contains(".")) 
    // convert to double 
else 
    // convert to integer 
没有例外


在执行上述操作之前,可以使用正则表达式检查字符串是否为数字。一种方法是使用模式
[0-9]+(\[0-9]){0,1}
。我不是正则表达式最好的,所以如果这是错误的,请纠正我

您可以尝试一系列嵌套的try捕获:

String input = textField[0].getText();
try {
    int textToInt = Integer.parseInt(input);
    // if execution reaches this line, it's an int
} catch (NumberFormatException ignore) {
    try {
        double textToDouble = Double.parseDouble(input);
        // if execution reaches this line, it's a double
    } catch (NumberFormatException e) {
        // if execution reaches this line, it's neither int nor double
    }
} 

您可以尝试一系列嵌套的try捕捉:

String input = textField[0].getText();
try {
    int textToInt = Integer.parseInt(input);
    // if execution reaches this line, it's an int
} catch (NumberFormatException ignore) {
    try {
        double textToDouble = Double.parseDouble(input);
        // if execution reaches this line, it's a double
    } catch (NumberFormatException e) {
        // if execution reaches this line, it's neither int nor double
    }
} 

解析和捕获异常?解析和捕获异常?那么“foo.bar”是一个有效的双精度值吗?我意识到并正在编辑我的答案,因为你发布了你的评论:)更好,但是1000位数字呢?Double有一个最大值和最小值。是的,你说得对。抛出并捕获数字格式异常确实更容易。。。我试过:(所以“foo.bar”是一个有效的双精度数字?当你发表评论时,我意识到并正在编辑我的答案:)更好,但是1000位数怎么样?Double有一个最大值和最小值。是的,你说得对。抛出并捕获数字格式异常确实更容易。。。我试过:(