Java string check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Petrosa
    New Member
    • Apr 2007
    • 9

    Java string check

    Hi. i am new to java and just starting to learn how it works.
    My problem now is, i need a very simple code, that shows a window, where u enter a string, and by pressing a button, it runs various checks on that strings (if it contains uppercase chars, digits, special case chars etc) and then displays a text with the results. Also, if possible a way to check if a string is included in a line of a text file
    I am still confused how to work with strings. I think it will be very informative to me. I checked the net and could'nt find a clear way to do this.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at this link. It contains the API documentation
    for all the code classes (including the String class). Have a look what that
    String class supplies you and try to figure out which method(s) is/are most
    suitable for the job.

    Note that you can download all that documentation (see the link in the top
    right corner). Do that and make it one of your favourites/bookmarks for easy
    retieval. You'll need it (at least I use it all the time ;-)

    kind regards,

    Jos

    Comment

    • Petrosa
      New Member
      • Apr 2007
      • 9

      #3
      Ok i found some web pages explaining the use of regex, and i also created the window i want. Now, i try and use the regex package, i write something like this

      import java.util.regex .*;

      ..............

      private void checkDigits(jav a.awt.event.Act ionEvent evt) {
      Regex r = new Regex("[0-9]");
      r.search(passwo rdField.getText ());
      if ( r.didMatch() )
      comments.setTex t("Your password Contains Digits");

      }


      Trying to compile, i get an error saying that i "Cannot find Symbol

      symbol : class Regex
      location: class NewJFrame
      Regex r = new Regex("[0-9]");

      What am i doing wrong?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Where did you read about a Regex class and a didMatch() method? They're
        definitely not in Java's core library.

        kind regards,

        Jos

        Comment

        • Petrosa
          New Member
          • Apr 2007
          • 9

          #5
          First i found it here
          http://www.javaregex.c om/tutorial.html

          then i also found it here
          http://java.sun.com/developer/technicalArticl es/releases/1.4regex/

          It starting to get realy confusing.... Is there any other way do what i am supposed to do?

          Comment

          • Petrosa
            New Member
            • Apr 2007
            • 9

            #6
            Hmmm i got a closer look and tried this

            boolean b = Pattern.matches ("0" passwordField.g etText());
            if (!b) comments.setTex t("Your password does not contain Digits");

            I get it to work but i cant make the pattern to work for the whole number range.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Petrosa
              First i found it here
              http://www.javaregex.c om/tutorial.html

              then i also found it here
              http://java.sun.com/developer/technicalArticl es/releases/1.4regex/

              It starting to get realy confusing.... Is there any other way do what i am supposed to do?
              For now only read the documentation from that link I gave you. All the other
              stuff you find using google etc. may not be part of the core classes API.

              Read the documentation for the Pattern class. It compiles a regular
              expression (RE) for you. Given a String the Pattern object can give you a
              Matcher that matches REs in that String. The RE for a non-zero
              sequence of digits is"[0-9]+".

              kind regards,

              Jos

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                ps. You wrote that you're a beginner. Regular expressions may be a bit over
                your head. Have a look at the Character class instead. It has a lot of
                utility functions that determine whether or not a char is a digit, uppercase etc.
                symbol.

                kind regards,

                Jos

                Comment

                • Petrosa
                  New Member
                  • Apr 2007
                  • 9

                  #9
                  Thx for the answers Jos.

                  I finally got it how to structure the patterns.

                  if ((Pattern.match es(".*\\p{Digit }.*", passwordField.g etText())) != true)
                  comments.setTex t(comments.getT ext() + "Your password does not contain Digits\n");

                  if ((Pattern.match es(".*[!£$%^&*@?<>+_].*", passwordField.g etText())) != true)
                  comments.setTex t(comments.getT ext() + "Your password does not contain Special Characters\n");

                  Now off to find how to make it work when looking through a dictionary and finds similar words etc

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Hats off. Well done! I didn't expect a beginner to get up and running in a single
                    day with those nasty Regular Expressions. If you dig deeper in them you'll
                    figure out that they're quite powerful up to a certain level and then you end up
                    using real full blown parsers ;-) All the best with your programming.

                    kind regards,

                    Jos

                    Comment

                    Working...