您如何识别字符条目是否为两位数或更多位数字?
How do you identify if a char entry is a two or more digit number?
我的意思是,如果我有一个由空格分隔的数组,我如何区分两个连续的字符是否是两个或更多数字?
请耐心等待,总的来说,我对编程还是很陌生。
这是我目前拥有的:
import java.util.*;
public class calc
{
public static String itemList;
public static String str;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
delimitThis();
sc.close();
}
public static void delimitThis()// Delimiter to treat variable and numbers as seperate
{
List<String> items = Arrays.asList(str.split("\s+"));
System.out.println(items);
for (int i = 0; i < str.length(); i++)
{
itemList = items.get(i);
category();
}
}
public static void category()////For Filtering between vars and constants and functions
{
for (int x = 0; x < itemList.length(); x++)
{
char ite = itemList.charAt(x);
if(Character.isDigit(ite))
{
System.out.println(ite + "is numeric"); //Will be replaced by setting of value in a 2 dimensional list
}
}
}
要检查字符串是否至少为 2 位数字,请使用此正则表达式:\d{2,}
.
public static boolean is2OrMoreDigits(String s) {
Pattern p = Pattern.compile("\d{2,}");
return o.matcher(s).matches();
}
首先,我要纠正你的错误:
错误一:
// bad
for (int i = 0; i < str.length(); i++)
{
itemList = items.get(i);
category();
}
您正在遍历 List<String> items
,但正在使用 str.length
。这是错误的。要{打印 item 然后对 items
中的 每个项目 执行 category()
},代码应为:
// fixed
for (int i = 0; i < items.size(); i++)
{
itemList = items.get(i);
category();
}
错误 2:
for (int x = 0; x < itemList.length(); x++)
{
System.out.println(itemList);
}
我不确定你想在这里做什么。只是你的代码对我来说没有意义。我假设您想打印行 来自 itemList
的每个字符 ,代码应该如下所示:
for (int x = 0; x < itemList.length(); x++)
{
System.out.println(itemList.charAt(x));
}
修正错误。现在检查一个字符串是否包含 2 位或更多数字,我们可以使用 String.matches()
和 regular expression:
if(itemList.matches("\d\d+")){
System.out.println(itemList + " is a two or more digit number");
}else{
System.out.println(itemList + " is NOT a two or more digit number");
}
代码最后看起来像这样:
import java.util.*;
public class Calc
{
public static String itemList;
public static String str;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
delimitThis();
sc.close();
}
public static void delimitThis()// Delimiter to treat variable and numbers as seperate
{
List<String> items = Arrays.asList(str.split("\s+"));
System.out.println(items);
for (int i = 0; i < items.size(); i++)
{
itemList = items.get(i);
category();
}
}
public static void category()////For Filtering between vars and constants and functions
{
for (int x = 0; x < itemList.length(); x++)
{
System.out.println(itemList.charAt(x));
}
// is 2 digit number or not?
if(itemList.matches("\d\d+")){
System.out.println(itemList + " is a two or more digit number");
}else{
System.out.println(itemList + " is NOT a two or more digit number");
}
}
}
我的意思是,如果我有一个由空格分隔的数组,我如何区分两个连续的字符是否是两个或更多数字? 请耐心等待,总的来说,我对编程还是很陌生。
这是我目前拥有的:
import java.util.*;
public class calc
{
public static String itemList;
public static String str;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
delimitThis();
sc.close();
}
public static void delimitThis()// Delimiter to treat variable and numbers as seperate
{
List<String> items = Arrays.asList(str.split("\s+"));
System.out.println(items);
for (int i = 0; i < str.length(); i++)
{
itemList = items.get(i);
category();
}
}
public static void category()////For Filtering between vars and constants and functions
{
for (int x = 0; x < itemList.length(); x++)
{
char ite = itemList.charAt(x);
if(Character.isDigit(ite))
{
System.out.println(ite + "is numeric"); //Will be replaced by setting of value in a 2 dimensional list
}
}
}
要检查字符串是否至少为 2 位数字,请使用此正则表达式:\d{2,}
.
public static boolean is2OrMoreDigits(String s) {
Pattern p = Pattern.compile("\d{2,}");
return o.matcher(s).matches();
}
首先,我要纠正你的错误:
错误一:
// bad
for (int i = 0; i < str.length(); i++)
{
itemList = items.get(i);
category();
}
您正在遍历 List<String> items
,但正在使用 str.length
。这是错误的。要{打印 item 然后对 items
中的 每个项目 执行 category()
},代码应为:
// fixed
for (int i = 0; i < items.size(); i++)
{
itemList = items.get(i);
category();
}
错误 2:
for (int x = 0; x < itemList.length(); x++)
{
System.out.println(itemList);
}
我不确定你想在这里做什么。只是你的代码对我来说没有意义。我假设您想打印行 来自 itemList
的每个字符 ,代码应该如下所示:
for (int x = 0; x < itemList.length(); x++)
{
System.out.println(itemList.charAt(x));
}
修正错误。现在检查一个字符串是否包含 2 位或更多数字,我们可以使用 String.matches()
和 regular expression:
if(itemList.matches("\d\d+")){
System.out.println(itemList + " is a two or more digit number");
}else{
System.out.println(itemList + " is NOT a two or more digit number");
}
代码最后看起来像这样:
import java.util.*;
public class Calc
{
public static String itemList;
public static String str;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
delimitThis();
sc.close();
}
public static void delimitThis()// Delimiter to treat variable and numbers as seperate
{
List<String> items = Arrays.asList(str.split("\s+"));
System.out.println(items);
for (int i = 0; i < items.size(); i++)
{
itemList = items.get(i);
category();
}
}
public static void category()////For Filtering between vars and constants and functions
{
for (int x = 0; x < itemList.length(); x++)
{
System.out.println(itemList.charAt(x));
}
// is 2 digit number or not?
if(itemList.matches("\d\d+")){
System.out.println(itemList + " is a two or more digit number");
}else{
System.out.println(itemList + " is NOT a two or more digit number");
}
}
}