Results 1 to 8 of 8
  1. #1
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default Quick Stupid Question

    Would the following code work? (I know I could test it out and will when I'm in front of a computer that has eclipse/netbeans. I'm currently at work and just don't have access to any programming environment and the curiosity is killing me!!!)


    So to satisfy my curiousity


    public class Test {


    public static void main (String[] args) {


    int x = 10;
    int y = 9;
    String condition = ">";

    if (x condition y) {

    System.out.println("This works");

    } //end if

    } //end main

    }//end class


    Thanks a bunch for the help! :)
    Last edited by bluekswing; 01-07-2008 at 09:24 PM. Reason: Added semi-colons

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Doesnot work .. where is semicolon .. the condition thing you are using is not accepted in java..
    dont worry newbie, we got you covered.

  3. #3
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    Thanks roots! Ugh - I thought I finally nailed a problem I've been having.

    I'm trying to create a list of "IF-Then" conditions regarding some varibles for java to check. I want to be able to dynamically create these conditions and not hard code them in some class. I'm attempting to create "rule objects" that can be stored in some List data structure and can be executed when needed.

    I"m having a terrible time figuring out how to construct such an object. ANy thoughts or suggestions?

    For example, I want one of the rules on the list (rule object) to be "If x > y add 1 to sum". Any thoughts suggestions?

    Thanks again for the help!

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Looks Something kinda Expression Parser.. pseudo language .. You can implement custom expression parser ..

    Can you list more of examples of such expressions .. possible expressions ..
    dont worry newbie, we got you covered.

  5. #5
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    Hmm interesting....I"ll have to read up more on expression parsing.


    Essentially I have an object that has 50 variables either of type String or type int. Everyday I want to be able to run a test on the object's instance variables based on a set of conditions. These conditions can change from day to day.

    All the rules will be of an "if- then" format. So,

    //If variable1 = variable2 then add 1 to total sum
    //If variable8 > variable3 then add 2 to total sum
    //If variable23 >= variable27 then subtract 0.5 from total sum
    //If variable19.equals(variable5) then add 0.25 to total sum


    So essentially every rule will contain some comparison between two variables where any of the following equality conditions apply: (> , < , = (.equals) , != , =<, >=)



    The stumbling block for me is how to create these rule objects.......

  6. #6
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    public static Object eval(String expression, Object... variables ){
     // Refer introductory compiler mechanisms 
    }
    Have a quick look at compilers .. that should get you started ..
    dont worry newbie, we got you covered.

  7. #7
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Comparison class

    Hello bluekswing.

    Why don't you just create a class that can compare two Double numbers. For example:
    Java Code:
    public class NumberCompare{
        public enum TOperation {equals, greater, smaller, greaterOrEqual, smallerOrEqual};
        private TOperation operation = TOperation.equals;
        public void setOperation(TOperation operation){
            this.operation = operation;
        }
        public TOperation getOperation(){
            return this.operation;
        }
        public boolean compare(Double first, Double second){
            switch (operation){
                case equals :
                    return first == second;
                    break;
                case greater:
                    return first > second;
                    break;
                case smaller:
                    return first < second;
                    break;
                case greaterOrEqual:
                    return first >= second;
                    break;
                case smallerOrEqual:
                    return first <= second;
                    break;
                default:
                    return false;
            }
        }
    }
    Now you can create a single NumberCompare object and change its operator as you need. Then you can call its compare() method. You can create a similar class for preforming the calculations. You can do the same for String objects.

    This code has been checked by hand. I hope this helped. :)
    Last edited by tim; 01-07-2008 at 10:31 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  8. #8
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    Thanks Tim and roots for the replies. You have given me much to think about....

Similar Threads

  1. a really stupid question
    By SwinGirl in forum NetBeans
    Replies: 10
    Last Post: 06-25-2008, 10:06 PM
  2. Wanna make 5 quick bucks?
    By GodiaN in forum Java Applets
    Replies: 5
    Last Post: 01-21-2008, 08:15 PM
  3. Quick Help Please! Can't Run Code!!
    By VinceGuad in forum Eclipse
    Replies: 4
    Last Post: 01-16-2008, 04:54 AM
  4. Quick Job required in Java
    By taxman in forum Jobs Offered
    Replies: 0
    Last Post: 01-02-2008, 12:46 PM
  5. Quick Question (Functions)
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 02:42 AM

Posting Permissions

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