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.
Related
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 {
....
}
For example I have the following block of code:
public String getDbSchema() {
return DB_SCHEMA;
}
Is there a shortcut to quickly turn this code into
public String getDbSchema() {
return properties.getProperty(DB_SCHEMA);
}
Currently I have to do properties.getproperty then take out right bracket and re-insert it into the end of the statement
When you select getProperty from the code completion, instead of pressing Enter, press the shortcut of Edit | Complete Current Statement (e.g. Ctrl+Shift+Enter), and DB_SCHEMA will be wrapped into call parentheses.
Sure, you can use a structural find and replace that is a little bit smart.
First, let's presume that this code has the form return XYZ; where XYZ is a constant identifier (CAPS or _)
Then you can go into search and replace in files (ctrl+shift+R), tick Case Sensitive and Regular Expression and enter:
Text to find: return ([A-Z_]*);
Replace with: return properties.getProperty($1);
Is this possible ? possible means, how to do it correctly? System.out.println("System.out.println("")");
No, you cannot. System.out.println() return type is void.
public void println()
When you write
System.out.println("System.out.println("")");
Compiler treats that the content inside "" as String not the function.
System.out.println() is not returning values(void) so you can't do something like you want here. BTW what is the purpose of doing this?
Again
System.out.println("System.out.println("")"); // this is not valid statement
you can write as follows
System.out.println("System.out.println(\"\")");
But out put is just
System.out.println("")
If you just want to print System.out.println("") then do like this
System.out.println("System.out.println(\"\")");
No. System.out.println is a void method. I'm not sure why you want to do that, just print one line after another.
no it is not possible Please check these link to know how the "system.out.print()" works
http://javapapers.com/core-java/system-out-println/
http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/print-statements.html
This depends on what you want. What result do you want to achieve? How much effort do you want to put into it?
What do you mean under nesting? The answer is: generally no, but if you really want, yes.
class SOP { // SOP stands for system.out.println
public static SOP p = new SOP();
public String toString() {return "";}
SOP p(Object... oo) {for(Object o : oo){System.out.print(o.toString());} return this;}
SOP pl(Object... oo) {p(oo); return l();}
SOP l() {System.out.println(); return this;}
}
public class A {
public static void main(String[] p) {
SOP.p.p("hello,").pl(" world!");
SOP.p.p("What exactly do you mean under \"nesting\"?", SOP.p.p("Is this nesting?"));
SOP.p.l();
}
}
and the output:
$ javac A.java
$ java A
hello, world!
Is this nesting?What exactly do you mean under "nesting"?
Have fun!
PS probably you want to write a program that prints itself.
In this case: the stuff in the quotes is not evaluated as Java code, and there's no way in java to do it... unless you call the compiler via a command of the underlying OS, provided that the compiler is there on the target platform.
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.
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.