Results 1 to 5 of 5

Thread: Phamtom period

  1. #1
    Join Date
    Sep 2015
    Location
    North Pole
    Posts
    3
    Rep Power
    0

    Default Phamtom period

    Every time I run my code, the output has a phantom period. I'm not sure how it got there. The program is supposed to find matches in a sentence(line 3) from an array of other words (line 4).

    This is my code.
    Java Code:
    class Source {
        public static void main (String[] args) {
        String Sentence = "This is a sentence ";
        String Words = "This apple corndog happy .";
        
        String[] parts = Sentence.split(" ");
        String[] parts2 = Words.split(" ");
        int length = parts.length;
        int length2 = parts2.length;
        
        for(int x = 0; x < length;x++){
            for(int z = 0; z < length2; z++){
                if (parts[x].matches(parts2[z])) {
                    System.out.println("\n" + parts2[z] + "; Match Found\n");
                        }
                else {
                System.out.println(parts[x] + "; " + parts2[z]);
                    }    
                }
            }
        }
    }
    output:
    This; Match Found

    This; apple
    This; corndog
    This; happy
    This; .
    is; This
    is; apple
    is; corndog
    is; happy
    is; .
    a; This
    a; apple
    a; corndog
    a; happy

    .; Match Found (Random period that is not in the line 3. This shouldn't be here.)

    sentence; This
    sentence; apple
    sentence; corndog
    sentence; happy
    sentence; .
    .; This
    .; apple
    .; corndog
    .; happy

    .; Match Found

    Can you tell me what is wrong with it?
    Last edited by MLG Used Car Salesman; 09-07-2015 at 02:37 AM. Reason: putting in the comments

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Phamtom period

    Can you add some comments to the print out explaining what is wrong with it and showing what it should be?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Phamtom period

    I believe it is related to String.matches() expecting a regular expression. The . says match any character.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Join Date
    Sep 2015
    Location
    North Pole
    Posts
    3
    Rep Power
    0

    Default Re: Phamtom period

    Quote Originally Posted by jim829 View Post
    I believe it is related to String.matches() expecting a regular expression. The . says match any character.
    Can you explain this more?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Phamtom period

    Look at the API doc for the matches() method of the String class. It has a link to regular expressions that explains some about how to make one.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. trial period
    By jperson in forum New To Java
    Replies: 0
    Last Post: 10-12-2011, 11:27 PM
  2. period function
    By newbiejava in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •