遍历具有正则表达式的数组不能按预期工作

时间:2017-05-09 18:30:40

标签: java arrays regex

简介:

在我的代码中,我提示用户选择一个月或选定的年份,然后将此值作为正则表达式包含在遍历包含气候测量的数组中 - 我已经从文本文件中扫描了该数据放入一个String-array。

Beneath是测量的样本,大约有。在我的文本文件中有1800个度量,我已检查数组是否设置正确,项目计数检查。

  

1874 Januar 3.2
1874 Februar   1.6
1874 Marts 2.9
......
2005 2005年10月11日1日2005年11月6日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])))

这两个条件不会将任何文本打印到控制台,因为我打算运行代码。

1 个答案:

答案 0 :(得分:0)

好的,测试过了。你可以使用String.contains(); https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence-

if(data [z] .contains(Integer.toString(choice)))                         的System.out.println(数据[Z]);