1 2 Previous Next 20 Replies Latest reply on Oct 13, 2009 3:24 PM by 3004 Go to original post
      • 15. Re: Parsing a date error.help with small program
        3004
        Implode wrote:
        I tried this, but I guess it doesnt check if the values fit into day month year

        pattern = Pattern.compile("\\d{2}\\d{2}\\d{2}");

        But i still get an array out of bounds error
        There's no way to know why you get that error when all you show is that pattern.

        As mentioned before: Provide an SSCCE. Include the exact, complete error message and indicate clearly exactly which line is causing it.
        • 16. Re: Parsing a date error.help with small program
          3004
          Implode wrote:
          Our posts are crossing.

          :(

          I have looked at all methods mentioned by members on this topic, but Im new to this and have no idea how to validate using simpleDateFormat.
          [http://www.google.com/search?q=java+simpledateformat+example]
          So maybe it is better to use try and catch after every token from the split string?
          I have no idea what you're talking about.
          But how could I tell the method that if an exception is found add the line to errorLine array, if no catch found then add to goodLine array?
          try {
            do something
            good.add(something)
          }
          catch (...) {
            errors.add(something);
          }
          try {
           do something else
            good.add(something else)
          }
          catch (...) {
            errors.add(something else);
          }
          • 17. Re: Parsing a date error.help with small program
            843789
            This is where the error is
                        String[] newToken = line.split("#");
            
                        pattern = Pattern.compile("ISBN\\x20(?=.{13}$)\\d{1,5}([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$");
                        Matcher matchIsbn = pattern.matcher(newToken[0]);
                        pattern = Pattern.compile("[\\d]+");
                        Matcher matchCopyNumber = pattern.matcher(newToken[1]);
                        pattern = Pattern.compile("[\\D&&\\w]+");
                        Matcher matchTitle = pattern.matcher(newToken[2]);
                        pattern = Pattern.compile("[\\D&&\\w]+");
                        Matcher matchAuthor = pattern.matcher(newToken[3]);
                        pattern = Pattern.compile("[\\d]+");
                        Matcher matchStatistics = pattern.matcher(newToken[4]);
                        pattern = Pattern.compile("\\d{2}\\d{2}\\d{2}");
                        Matcher matchBorrowDate = pattern.matcher(newToken[5]);
                        pattern = Pattern.compile("\\d{2}\\d{2}\\d{2}");
                        Matcher matchReturnDate = pattern.matcher(newToken[6]);
                        pattern = Pattern.compile("[\\d]+");
                        Matcher matchLibraryCardNumber = pattern.matcher(newToken[7]);
            Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
            at assignment2Program1.WriteFromBookFile.main(WriteFromBookFile.java:54)
            Java Result: 1

            the first line read from the file only has 7 tokens, token[6]

            0-321-26979-0#1#Java Software Solutions#9#080830#081130#90

            Edited by: Implode on Oct 12, 2009 4:18 PM
            • 18. Re: Parsing a date error.help with small program
              3004
              How well do you understand Java arrays?
              [http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]

              The error message is pretty clear. It's telling you exactly what's wrong and exactly where. On line 54 of WriteFromBookFile.java, you're trying to access the 8th element (at index 7) of an array that has at most 7 elements (at indices 0..6).

              You're assuming that split is giving you 8 or more tokens, but it's giving you 7 or fewer. So print out each of the elements of the split result.

              EDIT: And from your comments, it seems that you know that, so where is the confusion? Why, if you know that there are only elements 0..6, are you trying to access index 7? And why are you surprised that it doesn't work?

              Edited by: jverd on Oct 12, 2009 10:30 PM
              • 19. Re: Parsing a date error.help with small program
                843789
                jverd wrote:
                How well do you understand Java arrays?
                [http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]

                The error message is pretty clear. It's telling you exactly what's wrong and exactly where. On line 54 of WriteFromBookFile.java, you're trying to access the 8th element (at index 7) of an array that has at most 7 elements (at indices 0..6).

                You're assuming that split is giving you 8 or more tokens, but it's giving you 7 or fewer. So print out each of the elements of the split result.

                EDIT: And from your comments, it seems that you know that, so where is the confusion? Why, if you know that there are only elements 0..6, are you trying to access index 7? And why are you surprised that it doesn't work?

                Edited by: jverd on Oct 12, 2009 10:30 PM
                I understand arrays, but I am trying to explain the following.

                I need to read information from a text file.

                e.g. lines in file

                0-321-26979-9#4#Java Software Solutions#9#071002#071423#26
                0-13-089496-6#2#Business and information systems#Nickerson#15#000506
                0-13-089496-6#4#Business and information systems#Nickerson#15#000506#081109#92

                Each can have a possible 8 fields.

                Most of the lines don't, which is giving an array out of bounds error.

                That is what I would like to know, how can it be coded so that if there arent all the fields in a line, it moves to the next.

                A few more problems arise.

                Firstly, some of the information spans over 2 lines, so I cannot use nextLine().
                Secondly, different "lines" contain different fields. which means that i.e. token 4 in line 3 will be a different field to token 4 in line 6.

                It seems like the only way to correctly check each "group" of fields , is to check if the first token contains a 0 at the first char. but that doesnt solve the fact that different lines have fields in different position, so it cannot be matched?

                Does this make sense now?
                • 20. Re: Parsing a date error.help with small program
                  3004
                  Implode wrote:
                  I understand arrays, but I am trying to explain the following.

                  I need to read information from a text file.

                  e.g. lines in file

                  0-321-26979-9#4#Java Software Solutions#9#071002#071423#26
                  0-13-089496-6#2#Business and information systems#Nickerson#15#000506
                  0-13-089496-6#4#Business and information systems#Nickerson#15#000506#081109#92

                  Each can have a possible 8 fields.

                  Most of the lines don't, which is giving an array out of bounds error.

                  That is what I would like to know, how can it be coded so that if there arent all the fields in a line, it moves to the next.
                  If you understand arrays, then you know how to do this. It's simply a matter of making sure an element actually exists before trying to access it. If you can't figure out how to do that, go back through the arrays tutorial and see if it comes to you.
                  1 2 Previous Next