繁体   English   中英

如何检查双精度值是否包含特殊字符

[英]How to check if a double value contains special characters

我正在为一个程序编写代码,其中用户输入2到4个数字,最多可以长2个小数位。 但是,在用户输入这些数字之前,他们必须输入井号(例如,#12.34)。 我想知道如何检查输入的双精度值是否以井号开头,以及用户是否忘记输入该值,以再次提示他们再次输入。 到目前为止,我使用的是String值和'.startsWith()'代码,但后来我发现String值使程序的其余部分无法编写代码,因此我想将其保留为double值。 这是我目前拥有的代码,但希望更改为两倍代码:

String input;
System.out.print("Enter the 4 numbers with a pound key: ");
input = keyboard.next();
while (!input.startsWith("#")) {
 System.out.print("Re-enter the 4 numbers with a pound key: ");
 input = keyboard.next();
}

我想用前面提到的double替换String。

double值没有货币符号。 您应该在执行操作时检查货币符号,然后在解析双精度符号之前将其删除。

String input;
System.out.print("Enter the 4 numbers with a pound key: ");
input = keyboard.next();
while (!input.startsWith("£")) {
   System.out.print("Re-enter the 4 numbers with a pound key: ");
   input = keyboard.next();
}
doulble number = Double.parseDouble(input.substring(1));
String input;
System.out.print("Enter the 4 numbers with a pound key: ");
input = keyboard.next();
while (!input.startsWith("#")) {
 System.out.print("Re-enter the 4 numbers with a pound key: ");
 input = keyboard.next();
}
// if your excution reaches here. then it means the values entered by user is starting from '#'.
String temp = input;
double value = Double.parseDouble(temp.replace("#",""));

对于程序的其余部分,请使用value 我认为现在应该可以进行编码了。

应该使用startsWith方法。 我不知道你为什么这么说

稍后我发现String值使程序的其余部分无法编写代码

基本上,您要反复提示它,直到用户输入#号为止。 那么为什么不使用while循环呢?

System.out.println("Enter a number:");
String s = keyboard.next();
double d = 0.0; // You actually wnat to store the user input in a
                // variable, right?
while (!s.trim().startWith("#")) {
    System.out.println("You did not enter the number in the correct format. Try again.");
    s = keyboard.next();
    try {
        d = Double.parseDouble(s.substring(1));
    } catch (NumberFormatException ex) {
         continue;
    } catch (IndexOutOfBoundsException ex) {
        continue;
    }
}

大量的代码!

说明:

首先,它提示用户输入一个数字并将输入内容存储在s 这很容易理解。 现在来了困难的部分。 我们要循环播放,直到用户以正确的格式输入输入为止。 让我们看看那里有什么样的无效输入:

  • 用户未输入#号
  • 用户未输入数字

第一个由startsWith方法处理。 如果输入不是以#开头,请再次询问用户。 在此之前, trim首次调用剪掉空格中输入这样那样的输入" #5.90"是有效的。 第二部分由以下部分处理:

    try {
        d = Double.parseDouble(s.substring(1));
    } catch (NumberFormatException ex) {
         continue;
    } catch (IndexOutOfBoundsException ex) {
        continue;
    }

如果输入格式错误,请再次询问用户。 如果用户输入的字符串长度小于1,则再次询问用户。

“但是等等!为什么要调用substring方法?” 您询问。 如果用户确实在前面输入#,然后输入正确的数字格式,那么您将如何将该字符串转换为双精度? 在这里,我只是通过调用子字符串来剪掉第一个字符。

您可以尝试执行以下操作,删除所有不是数字或点的字符:

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input;
    System.out.println("Input a number: ");
    while (!(input = br.readLine()).equals("quit")) {
        Double parsedDouble;
        if (input.matches("#[0-9]+\\.[0-9]{1,2}")) {
            parsedDouble = Double.parseDouble(input.replaceAll("[^0-9.]+", ""));
            System.out.println("double: " + parsedDouble);
            System.out.println("Input another number: ");
        } else {
            System.out.println("Invalid. Try again. Example: #10.10");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM