[英]How do I identify 2 separate 2-digit numbers in a String?
我試圖找到一種方法來識別string
1或2位數字(不能有任何3位數字),並將它們加在一起,因此它們必須在80到95之間。
由於某種原因,該代碼無法正常工作,因為即使在理論上應該返回true,它也始終返回false。
恩。 “ Hi 57 how are you 30
”返回假
預先感謝您的幫助!
("line" is the name of the String.)
public boolean isDig(){
int total=0;
int h;
int length = line.length();
for(h=0; h < length-1; h++) {
if (Character.isDigit(line.charAt(h))){
if (Character.isDigit(line.charAt(h+1))){
if (Character.isDigit(line.charAt(h+2))){
return false;
}
else {
total= total+(line.charAt(h)+line.charAt(h+1));
h++;
}
}
else {
total= total+(line.charAt(h));
}
}
if (total>=80 && total<=95){
return true;
}
else {
return false;
}
}
代碼中的主要問題是line.charAt(h)
不是位置h
處的數字的數值。 它是代碼點值,例如'0'
為48。
獲取數值的最簡單方法是Character.getNumericValue(line.charAt(h))
,在其他地方也是如此。
您還缺少對中第一個數字乘以10的乘積。
假設您知道字符串是有效的,那么只需將字符串中的任何數字相加就很容易了。 從獲得總和的角度來看,它們是2位還是3位的事實並不重要。
int total = 0;
for (int i = 0; i < line.length(); ) {
// Skip past non-digits.
while (i < line.length() && !Character.isDigit(line.charAt(i))) {
++i;
}
// Accumulate consecutive digits into a number.
int num = 0;
while (i < line.length() && Character.isDigit(line.charAt(i))) {
num = 10 * num + Character.getNumericValue(line.charAt(i));
}
// Add that number to the total.
total += num;
}
您應該使用正則表達式進行這種解析:
public class Example {
public static void main(String[] args) {
String input = "Hi 57 how are you 30";
System.out.println(process(input));
}
private static boolean process(String input) {
Pattern pattern = Pattern.compile(".*?(\\d+).*?(\\d+)");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
int one = Integer.parseInt(matcher.group(1));
int other = Integer.parseInt(matcher.group(2));
System.out.println(one);
System.out.println(other);
int total = one + other;
return total >= 80 && total <= 95;
}
return false;
}
}
輸出:
57
三十
真正
使用正則表達式的一種可能解決方案。
public static boolean isValid(String str) {
// regular expression matches 1 or 2 digit number
Matcher matcher = Pattern.compile("(?<!\\d)\\d{1,2}(?!\\d)").matcher(str);
int sum = 0;
// iterate over all found digits and sum it
while (matcher.find()) {
sum += Integer.parseInt(matcher.group());
}
return sum >= 80 && sum <= 95;
}
讓一個java.util.Scanner
完成工作:
public boolean scan(String line) {
Scanner scanner = new Scanner(line);
scanner.useDelimiter("\\D+");
int a = scanner.nextInt();
int b = scanner.nextInt();
int sum = a + b;
return sum >= 80 && sum <= 95;
}
調用.useDelimiter("\\\\D+")
在與非數字字符匹配的正則表達式上對字符串定界,因此nextInt
查找下一個整數。 如果要提取負整數,則必須稍作調整。
您可以通過測試每個String元素上的Integer.parseInt()方法,將String轉換為Array並測試以查看String中的每個元素(以空格分隔)是否為Digit。 下面是一個示例:
public static boolean isDig(String theString) {
String[] theStringArray = theString.split(" ");
ArrayList<Integer> nums = new ArrayList<Integer>();
for(int x = 0; x < theStringArray.length; x++) {
String thisString = theStringArray[x];
try {
int num = Integer.parseInt(thisString);
nums.add(num);
}catch(NumberFormatException e) {
continue;
}
}
int total = 0;
for(int num: nums) {
total += num;
}
if(total >= 80 && total <= 95) {
return true;
}
else {
System.out.println(total);
return false;
}
}
我們首先根據空白將原始String拆分為Array。 然后,我們創建一個ArrayList,它將String中的每個數字添加到其中。 然后,我們創建一個for循環以查看Array中的每個單獨的String,並設置一個try-catch塊。 如果我們可以使用Integer.parseInt()方法將數字轉換為整數,則將其添加到ArrayList中。 如果沒有,我們將捕獲異常並使用“ continue”語句繼續循環。 一旦退出循環,我們可以創建一個名為“ total”的變量,並創建另一個for循環,以便將ArrayList中的每個數字加到總數上。 如果總數大於/等於80且小於/等於95,我們將返回True,否則將返回false。 讓我們測試一下代碼:
String digitTest = "There is a digit here: 50 and a digit here 45";
System.out.println(isDig(digitTest));
數字50和45應該等於95,我們的結果是:
true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.