Results 1 to 8 of 8
Thread: Quick Stupid Question
- 01-07-2008, 09:20 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
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
- 01-07-2008, 09:22 PM #2
Doesnot work .. where is semicolon .. the condition thing you are using is not accepted in java..
dont worry newbie, we got you covered.
- 01-07-2008, 09:32 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
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!
- 01-07-2008, 09:55 PM #4
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.
- 01-07-2008, 10:11 PM #5
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
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.......
- 01-07-2008, 10:26 PM #6Java Code:
public static Object eval(String expression, Object... variables ){ // Refer introductory compiler mechanisms }
dont worry newbie, we got you covered.
- 01-07-2008, 10:29 PM #7
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; } } }
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.
- 01-08-2008, 07:35 PM #8
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
a really stupid question
By SwinGirl in forum NetBeansReplies: 10Last Post: 06-25-2008, 10:06 PM -
Wanna make 5 quick bucks?
By GodiaN in forum Java AppletsReplies: 5Last Post: 01-21-2008, 08:15 PM -
Quick Help Please! Can't Run Code!!
By VinceGuad in forum EclipseReplies: 4Last Post: 01-16-2008, 04:54 AM -
Quick Job required in Java
By taxman in forum Jobs OfferedReplies: 0Last Post: 01-02-2008, 12:46 PM -
Quick Question (Functions)
By ibanez270dx in forum New To JavaReplies: 2Last Post: 11-16-2007, 02:42 AM
Bookmarks