Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: replacing few keywords with corresponding operators

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default replacing few keywords with corresponding operators

    Suppose i have a string as fallows
    if(a greater than b)
    and i want to replace "greater than" with ">",how should i replace?
    i want to replace the following keywords with corresponding operators if it appears in if statement.
    keyword operator
    greater than >
    lesser than <
    greater than or equals to >=
    lesser than or equals to <=
    not equals to !=
    equals to ==


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: replacing few keywords with corresponding operators

    Have a look at String.replace().

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: replacing few keywords with corresponding operators

    Ya i tried.I just want to know what concept i should use for the problem i stated.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: replacing few keywords with corresponding operators

    Please explain what you mean by "concept".
    Is the problem any more than replacing one String with another String?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: replacing few keywords with corresponding operators

    its about replacing only.but am not understanding how to do it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: replacing few keywords with corresponding operators

    To learn how to use a class's methods, write a short simple program with some Strings, use the replace() method and print out the results to see what it does. Experiment some until you understand how to use it and then you will be ale to use it in your program.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: replacing few keywords with corresponding operators

    I know how to use replace() method.my problem is when i read a string,how should i check if it contains the keywords i mentioned in post#1,and then replace with its corresponding operator.

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: replacing few keywords with corresponding operators

    Sounds like you're just trying to read in a String, loop through each operator and use the replace() method, then output the new string. Are we understanding correctly? Which part are you confused on?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: replacing few keywords with corresponding operators

    should i check if it contains the keywords
    The String class has methods for testing the contents of a String. Use one of them to do the checking.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: replacing few keywords with corresponding operators

    am trying to read a string and check if it contains the keywords am looking for.If it contains those keywords,i should replace it with operator.
    now to check if a string contains the keyword if need,i can't use .contains() method.so what should i do?

  11. #11
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: replacing few keywords with corresponding operators

    Unless your program requires you to know that it had a keyword after the fact, it is typically just easier to call the replace with each keyword then the end result will be correct. If your requirements state that you need to know what values were detected and replaced then you do need to use the contains method prior to each replace.

    --- Update ---

    Why cant you use the contains method?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  12. #12
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: replacing few keywords with corresponding operators

    class keys{
    public static void main(String[] args)
    {
    String str="if(a greater than b)";
    if(str.contains("greater than"))
    str=str.replace("greater than",">");
    if(str.contains("lesser than"))
    str=str.replace("lesser than", "<");
    .
    .
    .
    .
    }
    }
    i should use so many if blocks if the string contains more than one keywords and even if keywords are more.how can i reduce it?

  13. #13
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: replacing few keywords with corresponding operators

    If you are required to check if it contains the string before replacing it then you are doing it how you should. You could put all of your keywords and their replacements into a structure like a map then iterate through them instead of having them hardcoded.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  14. #14
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: replacing few keywords with corresponding operators

    ok i will try.but i don't know to use map,if u could post some links to learn about it,it would be helpful.

  15. #15
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: replacing few keywords with corresponding operators

    Check this out. He has some good examples.

    How to loop a Map in Java
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Output sentences using keywords in java
    By GameGirl91 in forum Collections and Generics
    Replies: 27
    Last Post: February 20th, 2013, 03:40 PM
  2. keywords
    By BLUEJ in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2013, 08:05 PM
  3. Can you name the Java keywords?
    By KevinWorkman in forum The Cafe
    Replies: 7
    Last Post: November 19th, 2011, 06:13 PM
  4. keywords
    By crazyjava in forum Java Theory & Questions
    Replies: 2
    Last Post: September 29th, 2010, 12:13 AM
  5. how to extract variables,keywords,operator...
    By Nanda in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2009, 10:19 PM