Results 1 to 7 of 7
  1. #1
    vimalaranjan is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default String Comparision

    String a = "my house is near the school";
    String b = "He Went to School";

    String c = JTextField.getText();

    for example if String c = school i must display the both a,b lines on JTextArea

    or if String c = house i must display only a lines on JTextArea

    how can i do this in Java

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Go to the Java API and read about the methods the String class has. One or more of them can help.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What have you tried so far?

  4. #4
    nitishsp is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Lightbulb

    try something like this:
    Java Code:
       String b = "my house is near the school";
            //String b = "my house is near the";
           // String b = "school is near the house";
            
            String c = "school";
            boolean match=false;
    
            for(int i=0;i<b.length()-c.length()+1;i++)
            {
                if(c.regionMatches(true,0,b,i,c.length()))
                {
                    match=true;
                    break;
                }
            }
    
            if(match)
                System.out.print(b);

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by nitishsp View Post
    try something like this:
    Please don't.

    @nitishsp: Please learn Java before trying to 'help' here. Your two posts so far are bad advice.

    db

  6. #6
    nitishsp is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Post

    Quote Originally Posted by DarrylBurke View Post
    Please don't.

    @nitishsp: Please learn Java before trying to 'help' here. Your two posts so far are bad advice.
    Hey, what's wrong with the code?
    It's working and with some modifications it can solve vimalaranjan's problem.

    By "try something like this:" I meant to say, "Use regionMatches(), It may solve your problem."

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by nitishsp View Post
    Hey, what's wrong with the code?
    It is clumsy; better use the indexOf( ... ) method or the contains( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  3. Secret of String references comparision.
    By Anjaneyulu in forum Advanced Java
    Replies: 15
    Last Post: 02-24-2010, 03:32 PM
  4. String comparision method problem
    By rons_sacramental in forum New To Java
    Replies: 7
    Last Post: 10-15-2009, 06:15 AM
  5. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 03:49 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
  •