如何检查双精度值是否包含特殊字符
How to check if a double value contains special characters
我正在为一个程序编写代码,在该程序中,用户可以输入 2 到 4 个数字,最多可以保留 2 个小数位。然而,在用户输入这些数字之前,他们必须输入英镑符号,例如#12.34。我想知道如何检查输入的双精度值是否以井号开头,如果用户忘记输入它,重新提示他们再次输入。到目前为止,我使用的是字符串值和“.startsWith()”代码,但后来我发现字符串值使程序的其余部分无法编码,因此我想将其保留为双精度值。这是我目前拥有的代码,但希望更改为双精度代码:
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 替换字符串。
double
值没有货币符号。您应该在执行操作时检查货币符号,并在解析 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
方法。我不知道你为什么这么说
I'm finding later on that a String value is making the rest of the program impossible to code
基本上,您希望重复提示它,直到用户输入# 符号。那么为什么不使用 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
以 trim 关闭输入中的空白,以便像 " #5.90"
这样的输入有效。第二个由这部分处理:
try {
d = Double.parseDouble(s.substring(1));
} catch (NumberFormatException ex) {
continue;
} catch (IndexOutOfBoundsException ex) {
continue;
}
如果输入格式错误,请再次询问用户。如果用户输入长度小于1的字符串,再询问用户。
"But wait! Why is there a call to the substring
method?" 你问了。如果用户在前面输入#,然后输入正确的数字格式,您将如何将该字符串转换为双精度数?在这里,我只是通过调用 substring trim 关闭第一个字符。
您可以尝试这样删除所有非数字或点的字符:
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");
}
}
}
我正在为一个程序编写代码,在该程序中,用户可以输入 2 到 4 个数字,最多可以保留 2 个小数位。然而,在用户输入这些数字之前,他们必须输入英镑符号,例如#12.34。我想知道如何检查输入的双精度值是否以井号开头,如果用户忘记输入它,重新提示他们再次输入。到目前为止,我使用的是字符串值和“.startsWith()”代码,但后来我发现字符串值使程序的其余部分无法编码,因此我想将其保留为双精度值。这是我目前拥有的代码,但希望更改为双精度代码:
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 替换字符串。
double
值没有货币符号。您应该在执行操作时检查货币符号,并在解析 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
方法。我不知道你为什么这么说
I'm finding later on that a String value is making the rest of the program impossible to code
基本上,您希望重复提示它,直到用户输入# 符号。那么为什么不使用 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
以 trim 关闭输入中的空白,以便像 " #5.90"
这样的输入有效。第二个由这部分处理:
try {
d = Double.parseDouble(s.substring(1));
} catch (NumberFormatException ex) {
continue;
} catch (IndexOutOfBoundsException ex) {
continue;
}
如果输入格式错误,请再次询问用户。如果用户输入长度小于1的字符串,再询问用户。
"But wait! Why is there a call to the substring
method?" 你问了。如果用户在前面输入#,然后输入正确的数字格式,您将如何将该字符串转换为双精度数?在这里,我只是通过调用 substring trim 关闭第一个字符。
您可以尝试这样删除所有非数字或点的字符:
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");
}
}
}