I am using the Xtend templates to write a small program. I try using the IF statement, but every time I execute it it prints the variable in the console, which I dont want it to.
«IF x==y»
The jump value is «database.get(2)»
«jump_var_letter = String.charAt(1)»
«old_jump_ahd=database.get(2) »
«ENDIF»
Here the database is an array of integers and String is an array of letters. Here I just want it to print the value found at database.get(2) i.e 5. The last two expressions befor the ENDIF is meant for assignning a few values( which need not be printed)
The jump value is 5
Instead I get
The jump value is 5
D
5
Could somebody please tell me how I could stop printing the other two values.
Thank you in advance for your help..
After looking for sometime on the net I found that you could prevent the printing of the expreesions in between by using block expressions and then returning a null expression. (Although this method is not encouraged, I found that it provides me the result I wanted). So the expression I posted could be written as:
«IF x==y»
The jump value is «database.get(2)»
«{jump_var_letter = String.charAt(1); "" }»
«{old_jump_ahd=database.get(2); ""} »
«ENDIF»
This prints
The jump value is 5.
Related
in a project I'm working right now, I need to parse escape sequences with the Scanner class (using Java in Linux). To include, for instance, the two END keys in the keyboard, I initially wrote the following code:
if(sc.findWithinHorizon("(\\G\\e\\[4~)?",0).length() > 0 || sc.findWithinHorizon("(\\G\\eOF)?",0).length() > 0 ) {
System.out.print("END"); //To see if it works
With that code, I don't get any output (the terminal just freezes). After seeing that, I separated the two condition in two different if's:
if(sc.findWithinHorizon("(\\G\\e\\[4~)?",0).length() > 0)
System.out.print("END");
else if(sc.findWithinHorizon("(\\G\\eOF)?",0).length() > 0 )
System.out.print("END");
Then it works. Does anybody know why it doesn't work with the OR operator?
Thanks in advance.
You'll want to capture what findwithinhorizon parses into a String then do your comparisons. What you're doing in the first if statement is parsing sc twice. Once doing a comparison for something then the second time doing a different comparison on a different part of the text.
I doubt this is the behavior you wanted. when you set horizon to 0 it's going to look at what is the current position then increment the position by one, so your second call is looking at a different index. If you truly have reached the end of the file I would believe this would cause some odd behavior and explain why it hangs.
I am attempting to eliminate a large amount of IF/ELSE statements from my code with the use of the ternary operator.
//Checks to see if person exists //if not then add //else print
println doesPersonExist(personName) ? addPerson(personName) : 'No such person'
The problem is that if the addPerson(personName) method is executed then NULL is printed due to the println. Is there anyway of NULL from printng to the console? I understand i could remove the println but then 'No such person' would not print.
UPDATE: I finally managed to get this working as required - i simply removed the println and also 'No such person' and replaced them with this:
doesCarrierExist(carrierName) ? exists() : addCarrier(carrierName)
Where the exists method simply called a println statement within the base class. I hope this helps anyone that has a similar problem in the future.
In your code you mix 2 different actions.
The print method.
The addPerson(personName).
And you wish to print that into the screen.
The best way is handling it as 2 separate handles one for each action (for the addition and for the printing).
If the addPerson(personName) returns a String result you can print to the console the result by using the following Groovy code section:
def textResult = doesPersonExist(personName) ? addPerson(personName) : ""
println(textResult)
Note: If you are using Java language replace the 'def' with 'String'.
I have the following code:
Integer b = 4;
System.out.println(b+++10);
Why does System.out.println evaluate that expression? If you look at Java's source code you will not find the code that evaluates this expression.
Also, why does b+++ work? I thought that there could only be two + after a variable.
Thanks in advance.
If you put the code in Netbeans, and format it (Alt-Shift-F) you will see this:
Integer b = 4;
System.out.println(b++ + 10);
So the result is 4 + 10 which is 14. The value of b is 5 after the command.
Let me quote #Rup here for your first question:
It's not really println that's evaluating it - if you assigned it to a variable and printed that you'd get the same result.
To add onto that, it's Java that's evaluating that expression. If you put that into any other function it would work as well. It's part of Java — it's called "nested expressions."
For your other question see #user000001's answer, and there was also a discussion in the comments that added onto that (I have edited it to make it more clearer):
It's not clear at all normally the result is 15. b has been incremented.
In other words why Integer b = 4; System.out.println(b++ ); gives 4 even though b has been incremented. – BenMansourNizar
#BenMansourNizar This is because you are using the postfix operator (see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html.) With b++ the value is first used, and then incremented. If you use ++b, the value will be incremented before being used. – user000001 Jun 2 '13 at 17:58
Thanks a lot, user0000001 :) - BenMansourNizar
I hope you and other people find this information helpful, I really just quoted a bunch of stuff and commented on it :P
I'm currently working on a basic parser in Java and I have run into a problem when it comes to loops. Say I want to parse a while loop it would look something like:
a = 0
WHILE a < 10
a = a + 1
WEND
This example contains 4 expressions one of which will only need to be parsed ones the others will need to be parsed 10 times. The first one being the initial a = 0, second and third being on each side of the condition in the WHILE statement and the last one being within the WHILE block.
When I wrote the interpreted for this it loads the expression class into 4 different variables as new, the reevaluates them for each time it runs.
My problem is that even though they are initially loaded as new classes each time, there is over flow between them so when I re-parse them all of them looks like the last one:
a + 1
Any advice on how to circumvent this?
The code base is rather big so I wont post it here but its available at: http://git.life-hack.org/basic-parser/tree/src/parser
The file I'm working in is LoopBlock.java
Hi I am having a problem with my while loop in the getAmatch() function
It doesn't enter the while loop in android but enters in a normal Java class.
All I can say off the top of my head without a bit more information is watch your case sensitivity on both input and patterns, and your while loop is a good piece of evidence; your matcher.find() is simply not finding a match. As it states in the documentation:
Parameters:
start: The index in the input at which
the find operation is to begin. If
this is less than the start of the
region, it is automatically adjusted
to that value. If it is beyond the end
of the region, the method will fail.
Returns:
true if (and only if)** a match has been found.