【发布时间】:2017-05-09 18:30:39
【问题描述】:
简介:
在我的代码中,我提示用户选择月份或年份,然后将该值作为正则表达式包含在遍历保存气候测量值的数组中 - 我已经从文本文件中扫描了放入一个字符串数组中。
下面是测量样本,大约有。我的文本文件中有 1800 个测量值,我检查了数组是否设置正确,项目计数检查。
1874 年 1 月 3.2
1874 年 2 月 1.6
1874 Marts 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;
}
}
- if (data[z].matches(Integer.toString(choice)))
- if (data[z].toString().matches((MONTH[choice])))
这两个条件不会将任何文本打印到控制台,因为我打算运行代码。
【问题讨论】:
-
这个sn-p好像漏掉了很多东西。 MONTH f.e.的声明在哪里?
-
将尽快修复。抱歉,我不确定是否有必要。
-
您是否有理由将您的内容用括号括起来,“("+choice+")” 而不是“choice”?我们也可以看看你的正则表达式模式吗?
-
是的,模式非常基本,年份是: (dddd) 其中 d 是数字。几个月以来它是: (dd) Appropo "("+choice+")" 这是我测试的应该满足正则表达式的标准。我使用了在线工具。
-
好的,但是你为什么要把'选择'放在括号里呢?如果 .txt 文件包含像“1874 Januar 3.2”这样的行并且您寻找(选择),f.e. (1874 年),它不会工作?