Results 1 to 2 of 2
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default Checking for forbidden words

    I'm trying to put together a method that check for predefined vars. If a user tries to use a predefined variable not mapping is supposed to take place.

    Method to check for predefined.
    Java Code:
       static boolean isVarAllowed()
       {
          if(st.getWord() == "e" ||          //getWord() returns String 
             st.getWord() == "pi" ||
             st.getWord() == "sin" ||
             st.getWord() == "cos" ||
             st.getWord() == "log" ||
             st.getWord() == "exp" ||
             st.getWord() == "ans")
          {
             return false; 
          }
          else
          {
             return true;
          }
       }
    Usage:
    Java Code:
            if(isVarAllowed() == true)
             {
                System.out.println("Test: It's mapping.");
                map.put(st.getWord(), new Double(res));
             }
    The mapping works, but I can't get it do deny over mapping. As it is now I'm loosing stuff like pi if the user tries to save stuff in pi. I other words, all mappings are allowed now. Well, as I said, that's not what I what. :-/
    Last edited by überfuzz; 04-15-2011 at 11:46 AM. Reason: Stupid title

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Don't compare Strings for equality like that: use the equals() method. Also, why don't you use a simple Set that contains the forbidden words?

    Java Code:
    private static final Set<String> forbidden= new HashSet<String>() {{
       add("e");
       add("pi");
       ...
    }};
    
    private isMappable(String word) {
       return !forbidden.contains(word);
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. how to do checking value
    By madhuks in forum Java Servlet
    Replies: 3
    Last Post: 07-21-2010, 09:35 AM
  2. Checking udppackets in xp
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 05-25-2010, 03:48 AM
  3. Checking if a file name already has .txt in it
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 12:45 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
  •