Java regex checker not working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sort { public static void main(String[] args) { Scanner input = new Scanner(System.in); Boolean checker = false; String regex ="^(?:([0-9])(?!.*\1))*$"; Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE); System.out.println("Enter the Year:"); String number = input.next(); int year = Integer.valueOf(number); Matcher matcher; while(!checker) { matcher = pattern.matcher(number); if(matcher.find()) checker = true; else year++; number = Integer.toString(year); } System.out.println(number); } } |
我和我的朋友正在尝试创建一个算法来计算输入的数字之后的下一个数字,这个数字不会重复(例如
兴趣代码是:
1 2 3 4 5 6 7 8 9 | Matcher matcher; while(!checker) { matcher = pattern.matcher(number); if(matcher.find()) checker = true; else year++; number = Integer.toString(year); } |
由于某种原因,即使
什么是错误的,我们如何改变我们的逻辑,以便当数字中的数字重复时,检查器返回false?
考虑这些线条
1 2 3 4 5 |
有一些问题。我想你缺少牙套了。当
1 2 3 4 5 | checker = matcher.find(); if (!checker) { // checker is not true... e.g. checker == false. year++; number = Integer.toString(year); // I assume you wanted this here. } |