The string variable result
has the following string:
字符串变量result具有以下字符串:
All Prime Numbers Up To {10}:
1 2 3 4 5 6 7 8 9
When I use this code to scan each numerical token and return its length:
当我使用此代码扫描每个数字标记并返回其长度时:
Scanner scan = new Scanner(result);
while (scan.hasNext()) {
String num = scan.next();
if (num.matches(".*\\d.*")) {
System.out.print("number: ");
System.out.println(num);
System.out.print("The length: ");
System.out.println(num.length());
System.out.println("----");
}
}
The result shows the following:
结果显示如下:
Please Enter a Number: 9
number: {9}:
The length: 4
----
number: 1
The length: 1
----
number: 2
The length: 10
----
number: 3
The length: 10
----
number: 4
The length: 1
----
number: 5
The length: 10
----
number: 6
The length: 1
----
number: 7
The length: 10
----
number: 8
The length: 1
----
number: 9
The length: 1
----
Why it is not printing the correct length of each token of numbers? Even though it prints it correctly!
为什么不打印每个数字标记的正确长度?即使它正确打印!
2 个解决方案
#1
How do you initialize the variable result?
如何初始化变量结果?
String result = "All Prime Numbers Up To {10}:\n1 2 3 4 5 6 7 8 9";
Using the init above I get a correct length of 1 for all numbers 1-9. I would use something like this:
使用上面的init,我得到所有数字1-9的正确长度1。我会用这样的东西:
String result = "All Prime Numbers Up To {10}:\n1 2 3 4 5 6 7 8 9";
Scanner scan = new Scanner(result);
while (scan.hasNext()) {
if (scan.hasNextInt()) {
Integer num = scan.nextInt();
System.out.println("number: "+num);
System.out.println("The length: "+num.toString().length());
System.out.println("----");
}
else {
scan.next();
}
}
scan.close();
#2
I figured out the reason thanks to @KickButtowski. The problem was with my regex.
我想出了@KickButtowski的原因。问题在于我的正则表达式。
I had to change it to the following: "[0-9]+"
then it worked!
我不得不将其更改为以下内容:“[0-9] +”然后它才有效!
Even though my previous code was not wrong, this regex solved another problem I had in my code. If I used the regex ".*\\d.*"
it is going to return numbers that is mixed with characters, and I didn't want that.
即使我之前的代码没有错,这个正则表达式解决了我在代码中遇到的另一个问题。如果我使用正则表达式“。* \\ d。*”它将返回与字符混合的数字,我不想要那个。
#1
How do you initialize the variable result?
如何初始化变量结果?
String result = "All Prime Numbers Up To {10}:\n1 2 3 4 5 6 7 8 9";
Using the init above I get a correct length of 1 for all numbers 1-9. I would use something like this:
使用上面的init,我得到所有数字1-9的正确长度1。我会用这样的东西:
String result = "All Prime Numbers Up To {10}:\n1 2 3 4 5 6 7 8 9";
Scanner scan = new Scanner(result);
while (scan.hasNext()) {
if (scan.hasNextInt()) {
Integer num = scan.nextInt();
System.out.println("number: "+num);
System.out.println("The length: "+num.toString().length());
System.out.println("----");
}
else {
scan.next();
}
}
scan.close();
#2
I figured out the reason thanks to @KickButtowski. The problem was with my regex.
我想出了@KickButtowski的原因。问题在于我的正则表达式。
I had to change it to the following: "[0-9]+"
then it worked!
我不得不将其更改为以下内容:“[0-9] +”然后它才有效!
Even though my previous code was not wrong, this regex solved another problem I had in my code. If I used the regex ".*\\d.*"
it is going to return numbers that is mixed with characters, and I didn't want that.
即使我之前的代码没有错,这个正则表达式解决了我在代码中遇到的另一个问题。如果我使用正则表达式“。* \\ d。*”它将返回与字符混合的数字,我不想要那个。