我的意思是,如果我有一个由空格分隔的数组,我如何区分两个连续的字符是否是两个或更多个数字? 跟我一起,我对编程一般都很陌生。
这是我到目前为止所做的:
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
}
}
}
答案 0 :(得分:1)
首先,我想解决你的错误:
错误1:
// bad
for (int i = 0; i < str.length(); i++)
{
itemList = items.get(i);
category();
}
您正在浏览List<String> items
,但正在使用str.length
。这是错误的。要为category()
中的每个项目 {打印项然后items
},代码应为:
// 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");
}
}
}
答案 1 :(得分:0)
要检查字符串是否至少为2位数字,请使用此正则表达式:Set<CompletableFuture<String>> futures = service.getSomething();
for (CompletableFuture<String> future : futures) {
System.out.println(future.get());
}
。
Set<CompletableFuture<String>> futures = service.getSomething();
Set<CompletableFuture<String>> donefutures = new HashSet<>();
while (true) {
if (futures.equals(donefutures)) {
break;
}
futures
.stream()
.filter(f -> !donefutures.contains(f))
.filter(CompletableFuture::isDone)
.peek(donefutures::add)
.map(f -> {
try {
return f.get();
} catch (InterruptedException | ExecutionException e) {
return null;
}
})
.forEach(System.out::println);
Thread.sleep(100);
}