尝试使用java打印出具有特定格式的字符串时出错

时间:2014-04-25 19:45:48

标签: java nullpointerexception match

我正在阅读包含以下内容的文件:

127.0.0.1:8080

127.a.0.10:8081

127.0.1:8080.1

127.0.10.5:-8080

从那里我必须打印出符合以下格式的字符串。 ###。#。#。#:####此#表示任何正整数。 当我运行代码时,它打印出第一行,语句"无效"对于接下来的三个字符串,但它也标记了一个错误。

线程中的异常" main"显示java.lang.NullPointerException     在homework4.Main.main(Main.java:36)

我该如何解决这个问题?是否与inputStream = null有关? 提前谢谢。

            public static void main(String[] args) 
            {
                Scanner inputStream = null;
                int count1 = -1;
                int i = 0;
                final String[] content = new String[200];
                {
                    try
                    {
                        inputStream = 
                                new Scanner (new FileInputStream ("input.txt"));
                    }
                    catch (FileNotFoundException e)
                    {
                        System.out.println("File input.txt could not be found or could not be opened");
                        System.exit(0);
                    }
                }
                while (inputStream.hasNext())
                {
                    content[++count1] = inputStream.nextLine();
                }


                for (i=0; i <= content.length ; i++)
                {


                    if ( content[i].matches("^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}\\:\\d{4}"))
                    {

                        System.out.println(content[i]);
                    }

                    else 
                    {
                        System.out.println("Not Valid");
                    }
                }
            }}

4 个答案:

答案 0 :(得分:0)

if (content[i] != null) {   //check for this
    if (content[i].matches("^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}\\:\\d{4}")) { ... }
    else { ... }
}

答案 1 :(得分:0)

如果您的目的只是打印,那么您不需要存储并且不需要String[] content ..只需阅读该行并验证该行是否符合特定模式。

将条件移至while循环。

 while (inputStream.hasNext())
   {
         String content = inputStream.nextLine();
          // Move the condition and check with content variable.
               if ( content.matches("^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}\\:\\d{4}"))
                {

                    System.out.println(content);
                }

                else 
                {
                    System.out.println("Not Valid");
                }
   }

如果您的目的是存储内容。如果你不知道前面的行的大小/数量,请使用List来存储哪些可以根据需要动态增长。

喜欢

List<String> contents = new ArrayList<String>() 

并添加有效条件的有效行。

  contents.add(content);

答案 2 :(得分:0)

    Path path = Paths.get("input.txt");
    List<String> strings = Files.readAllLines(path, Charset.defaultCharset());
    for(String string: strings) {
        if (string.matches("^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}:\\d{4}")) {
            System.out.println(string);
        } else {
            System.out.println("not valid");
        }
    }

如果您使用java 8,则可以使用Streams API

    Path path = Paths.get("input.txt");
    List<String> strings = Files.readAllLines(path);
    strings.stream()
            .forEach(string -> {
                if (string.matches("^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}\\:\\d{4}")) {
                    System.out.println(string);
                } else {
                    System.out.println("not valid");
                }
            });

注意:你的正则表达式包含冗余转义

使用

^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}:\\d{4}

而不是

^\\d{3}\\.\\d{1}\\.\\d{1}\\.\\d{1}\\:\\d{4}

答案 3 :(得分:-1)

在你的for循环中,你从0变为长度。最后一个索引是(长度 - 1)

另一个问题是如果您的文件超过200行

相关问题