Java 使用正则表达式遍历数组不会';我不能如愿工作 简短介绍:
在我的代码中,我会提示用户选择一个月或一个年,然后将该值作为正则表达式包含在遍历包含气候测量值的数组中,我已经从文本文件中扫描了该数组并将其放入字符串数组中 下面是一个测量示例,我的文本文件中大约有1800个测量值,我已检查阵列是否正确设置,项目计数是否正确 1874年1月3.2Java 使用正则表达式遍历数组不会';我不能如愿工作 简短介绍:,java,arrays,regex,Java,Arrays,Regex,在我的代码中,我会提示用户选择一个月或一个年,然后将该值作为正则表达式包含在遍历包含气候测量值的数组中,我已经从文本文件中扫描了该数组并将其放入字符串数组中 下面是一个测量示例,我的文本文件中大约有1800个测量值,我已检查阵列是否正确设置,项目计数是否正确 1874年1月3.21874年2月 1.61874个集市2.9…2005年奥克托伯11.12005年11月6.32005年12月2.7 我已经在下面包含了一段代码示例——在最底层,我已经指出了我从调试器知道的代码没有按预期工作 编辑:发布完
1874年2月 1.6
1874个集市2.9
…
2005年奥克托伯11.1
2005年11月6.3
2005年12月2.7
我已经在下面包含了一段代码示例——在最底层,我已经指出了我从调试器知道的代码没有按预期工作 编辑:发布完整代码而不是样本
public class Klimadata {
public static String[] data;
private static boolean choose_year;
private static boolean choose_month;
private static int yearChoice;
private static int monthChoice;
private static String[] MONTH = { "", "Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August",
"September", "Oktober", "November", "December" };
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
data = init();
while ((choose_year == false) || (choose_month == false)) {
System.out.println("Choose Month or Year (m/y)");
String choice = scan.nextLine();
if (choice.equalsIgnoreCase("y")) {
choose_year = true;
System.out.println("Chosen year, now enter which (int, 1874-2005):");
choice = scan.nextLine();
while (true) {
yearChoice = Integer.parseInt(choice);
if ((yearChoice >= 1874) && (yearChoice <= 2005)) {
printData(yearChoice);
System.out.println("Program exiting.");
System.exit(0);
} else {
System.out.println("Please choose a year, use 4 digits.");
choice = scan.nextLine();
}
}
}
else if (choice.equalsIgnoreCase("m")) {
choose_month = true;
System.out.println("Chosen month, now enter which (int, 1-12):");
choice = scan.nextLine();
while (true) {
monthChoice = Integer
.parseInt(choice); /* Casted from String to int */
if (((monthChoice >= 1) && (monthChoice <= 12))) {
printData(monthChoice);
System.out.println("Program exiting.");
System.exit(0);
} else {
System.out.println("Please choose a month, use 1 or 2 digits.");
choice = scan.nextLine();
}
}
}
else {
System.out.println("Neither m or y has been chosen, try again!");
}
}
scan.close();
}
/* Prints our selection to the terminal */
private static void printData(int choice) {
/* Year has been chosen */
if (choice > 1000) {
for (int z = 0; z < data.length; z++) {
if (data[z].matches(Integer.toString(choice)))
System.out.println(data[z]);
}
}
/* Month has been chosen */
else if (choice <= 12) {
for (int z = 0; z < data.length; z++) {
if (data[z].toString().matches((MONTH[choice])))
System.out.println(data[z]);
}
}
}
@SuppressWarnings("resource")
/* Initializes an array of the weather data */
public static String[] init() throws FileNotFoundException {
String temp = "";
/* Imports the data into a String */
Scanner scan = new Scanner(new FileReader("C:\\Users\\Timber\\workspace\\Codejudge_5\\src\\vejrdata.txt"));
while (scan.hasNext()) {
temp = temp.concat(scan.nextLine() + "\n");
}
/* Splits the String into a String[] */
data = temp.split("[\\r\\n]+");
System.out.println(Arrays.toString(data));
return data;
}
}
公共类Klimadata{
公共静态字符串[]数据;
私人静态布尔选择(u年);;
私有静态布尔选择_月;
私人静态选择;
私人静态int monthChoice;
私有静态字符串[]月={”、“一月”、“二月”、“集市”、“四月”、“主要”、“六月”、“朱莉”、“八月”,
“九月”、“奥克托伯”、“十一月”、“十二月”};
公共静态void main(字符串[]args)引发FileNotFoundException{
扫描仪扫描=新扫描仪(System.in);
data=init();
while((选择_year==false)| |(选择_month==false)){
系统输出打印项次(“选择月或年(m/y)”;
字符串选择=scan.nextLine();
if(选择相等信号情况(“y”)){
选择_year=true;
System.out.println(“所选年份,现在输入(int,1874-2005):”;
choice=scan.nextLine();
while(true){
yearChoice=Integer.parseInt(选项);
如果((yearChoice>=1874)和&(yearChoice=1)和&(monthChoice 1000){
对于(int z=0;z 否则,如果(choiceOk),测试它。您可以只使用String.contains();
if(数据[z].包含(整型.toString(选项)))
System.out.println(data[z]);这段代码似乎遗漏了很多东西。f.e.月的声明在哪里?将尽快修复。抱歉,我不确定是否有必要。是否有理由将内容包装在括号中,(“+choice+”),而不仅仅是“choice”?我们也可以看到你的正则表达式模式吗?是的,模式非常基本,年份是:(dddd)其中d是一个数字。月份是:(dd)Appropo“(“+choice+”)”这是我测试的,应该满足正则表达式的标准。我使用了一个在线工具。好的,但是为什么你要用parantises包装“choice”?如果.txt文件包含这样的行“1874年1月3.2日”,你在寻找(选择),f.e.(1874),它不会起作用吗?