So I have this little piece of code:
System.out.println("Size: " + mounts.keySet().size());
for (JHttpPath entry : mounts.keySet()) {
System.out.println("Got one: " + entry.getPath());
if (entry.getDomain().equalsIgnoreCase(path.getDomain()) && entry.getPath().equalsIgnoreCase(path.getPath())) {
System.out.println("WIN! " + entry.getPath());
return mounts.get(entry);
}
}
And this is the output:
Size: 4
Got one: /host
But it should be:
Size: 4
Got one: /host
Got one: /cookie
Got one: /
Got one: /
Any idea on what is blocking the statement :P
Well, as this is commercial software I can't provide one of the classes.
This may seem very like a beginner question but it is actually very odd.
The problem: The String where I used .equals(IgnoresCase) was defined as NULL, this resulted in an exception which is ignored on purpose by my software.
My bad, thanks for all input.
Solution code:
(entry.getDomain() == null || path.getDomain() == null || entry.getDomain().equalsIgnoreCase(path.getDomain()))
The obvious answer is that one of your methods executed after the Got one line is printed is entering into an endless loop. GetDomain() from the looks of it.
Maybe if we knew a bit more about JHttpPath ? I assume it's a custom class, as Google only turns up this very question when Googling JHttpPath.
Related
I'm trying to set-up clang-format so that it confirms to Oracle's Java Code Conventions. The document provides numerous examples, of which the most important are:
function(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5);
var = function1(longExpression1,
function2(longExpression2,
longExpression3));
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6;
if ((condition1 && condition2)
|| (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
The following options cover the first three cases:
AlignAfterOpenBracket: Align
AlignAfterOpenBracket: Align
BreakBeforeBinaryOperators: true
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
ColumnLimit: 80
IndentWidth: 4
TabWidth: 8
About the fourth case, the document says: "Line wrapping for if statements should generally use the 8-space rule, since conventional (4 space) indentation makes seeing the body difficult." I went through all options, but I don't think I came across something.
Its admittedly a rather fastidious issue, but I thought maybe there is something I overlooked. Any ideas would be greatly appreciated. Thanks in advance!
I can’t solve this task without a string (don’t know yet) :
"My program asks the user if he wants to see a smiley. If he answers with 'Y' he gets a ":)", other input will be a ":(". Use a conditional operator."
My solution (with a string):
System.out.println("Do you want to see a smiley");
answer=scan.findWithinHorizon(".",0).charAt(0);
string=(answer=='Y')?: ":)" : ":("; //works like that but I need it without string
System.out.println(string);
btw: is the conditional operator often used?
Thanks for your help
And if there are any further advices tell me please.
I don't know if i understan you but you can try:
if(answer=='Y'){
System.out.println(":)");
}
else{
System.out.println(":(");
}
And yes conditional operator for example: if/else is one of the basic things in programing.
Do you mean without String variables? Then here is the nasty oneliner:
System.out.println("Do you want to see a smiley");
System.out.println(scan.findWithinHorizon(".",0).charAt(0)=='Y' ? ":)" : ":(" );
If you mean without using any kind of string (not even ""), you cold print each char individually. This would not require a String but is really annoying and unnecessary.
Edit: because requested, here is this version:
System.out.print('D');
System.out.print('o');
....
System.out.print('y');
System.out.print('\n');
if (scan.findWithinHorizon(".",0) == 'Y') {
System.out.print(':');
System.out.print(')');
System.out.print('\n');
} else {
....
}
This is only sample code.My point is to make: 'If Hello OR foo word is found, do something'.But while loop does not react, even if both strings are in text.If I use only one condition without || while loop does what I expect.How cant I fix this? Thank you!
public void start(){
Document doc=Jsoup.connect("http://www.yahoo.com").get();
String text=doc.text();
while(!text.contains("Hello")||!text.contains("foo"))
System.out.println("Not found.");
}
}
You have some operator precendence issues.
Right now, you're saying if text doesn't contain hello OR it doesn't contain foo do the loop; Use
while(!text.contains("Hello")&&!text.contains("foo"))
instead. This means "if text doesn't contain hello AND doesn't contain foo repeatedly flood System.out with "not found" until the user kills your program or the JVM dies".
You should change your code as follows
while(!text.contains("Hello")&&!text.contains("foo"))
System.out.println("Not found.");
}
}
You can also do:
while(!(text.contains("Hello") || text.contains("foo"))){...}
Maybe it's what you were trying to do above.
So I'm just beginning to tinker with Drools and am enjoying it,tough the documentation(the bits I've found at least) is a bit deep end all over.
I'm trying to create a rule that will fire when two objects share an attribute but can't seem to get the condition right. If I'm reading the documentation right, this should work:
(Yes, I am using Magic:The Gathering rules as a base for playing around because I know them well)
rule "704.5j. If two or more planeswalkers that share a planeswalker type are on the battlefield, all are put into their owners' graveyards. This is called the 'planeswalker uniqueness rule'."
when
$c1 : Card (CurrentZone == ZoneType.Battlefield , Types.contains("Planeswalker") , $subtype : Types.get(1) , $c1ID : ID );
$c2 : Card (CurrentZone == ZoneType.Battlefield , Types.contains("Planeswalker") , Types.contains($subtype) , ID != $c1ID);
then
System.out.println("PW Uniqueness: " + $c1.getName() + " | " + $c2.getName());
$c1.setCurrentZone(ZoneType.Graveyard);
$c2.setCurrentZone(ZoneType.Graveyard);
end
Will I have to do this on the java side of things?
EDIT: Also, tutorial/guide suggestions for Drools are extremely welcome.
If you havent got this working, maybe moving a section to an IF in the Then section as to avoid declaring stuff in when and then recalling it in the same section,
rule "704.5j. If two or more planeswalkers that share a planeswalker type are on the battlefield, all are put into their owners' graveyards. This is called the 'planeswalker uniqueness rule'."
when
$c1 : Card (CurrentZone == ZoneType.Battlefield , Types.contains("Planeswalker") ,($subtype : Types.get(1)));
$c2 : Card (CurrentZone == ZoneType.Battlefield , Types.contains("Planeswalker") ,($subtype2 : Types.get(1)) );
then
if (($subtype == $subtype2) && ($c1.getID() == $c2.getID()))
{
System.out.println("PW Uniqueness: " + $c1.getName() + " | " + $c2.getName());
$c1.setCurrentZone(ZoneType.Graveyard);
$c2.setCurrentZone(ZoneType.Graveyard);
}
end
This is a little long winded. but i usually avoid declaring and checking against the same variables in the When, i declare everything i need (using arrays and evals most the time) and then check with IF statments within the Then.
Also as # Marko said, get the first attribute down, check its working and just add one check at a time. sometimes i build big rules using stupid amounts of IFs etc. once its working. i start to bring the size down. its easier to reduce a working things, than repair a small broken thing :)
I would like to be able to evaluate an boolean expression stored as a string, like the following:
"hello" == "goodbye" && 100 < 101
I know that there are tons of questions like this on SO already, but I'm asking this one because I've tried the most common answer to this question, BeanShell, and it allows for the evaluation of statements like this one
"hello" == 100
with no trouble at all. Does anyone know of a FOSS parser that throws errors for things like operand mismatch? Or is there a setting in BeanShell that will help me out? I've already tried Interpreter.setStrictJava(true).
For completeness sake, here's the code that I'm using currently:
Interpreter interpreter = new Interpreter();
interpreter.setStrictJava(true);
String testableCondition = "100 == \"hello\"";
try {
interpreter.eval("boolean result = ("+ testableCondition + ")");
System.out.println("result: "+interpreter.get("result"));
if(interpreter.get("result") == null){
throw new ValidationFailure("Result was null");
}
} catch (EvalError e) {
e.printStackTrace();
throw new ValidationFailure("Eval error while parsing the condition");
}
Edit:
The code I have currently returns this output
result: false
without error. What I would like it to do is throw an EvalError or something letting me know that there were mismatched operands.
In Java 6, you can dynamically invoke the compiler, as explained in this article:
http://www.ibm.com/developerworks/java/library/j-jcomp/index.html
You could use this to dynamically compile your expression into a Java class, which will throw type errors if you try to compare a string to a number.
Try the eval project
Use Janino! http://docs.codehaus.org/display/JANINO/Home
Its like eval for java
MVEL would also be useful
http://mvel.codehaus.org/
one line of code to do the evaluation in most cases:
Object result = MVEL.eval(expression, rootObj);
"rootObj" could be null, but if it's supplied you can refer to properties and methods on it without qualificiation. ie. "id" or "calculateSomething()".
You can try with http://groovy.codehaus.org/api/groovy/util/Eval.html if groovy is an option.