Can i nest two output statement in Java - java

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.

Related

Possible without String?

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 {
....
}

How to compare a element in String array in Android?

I have a String array as follows:
String [] str_cmd_arr={"cmd1", "cmd2"};
Given that, "cmd1" will output "perform command 1", while "cmd2" will output "perform command 2".
From my str_cmd_arr, how can I print the outputs individually in Java/Android? Currently, I am using this code
for (int i=0;i<str_cmd_arr.length;i++){
if(i<1){
Log.d("TAG","perform command 1");
}
else{
Log.d("TAG","perform command 2");
}
}
The real solution here: use a Map, like
Map<String, String> commandsAndOutput = new HashMap<>();
commandsAndOutput.put("cmd1", "cmd1 output");
...
to later do
String output = commandsAndOutput.get("cmd1");
for example.
Another, probably more sane way here: consider using enums, like:
public enum Command {
CMD1, CMD2;
}
if you are looking for more "compile time" support when making choices between different commands. As you now can write down:
Command cmd = ...
switch(cmd) {
case(CMD1) : ...
But another word of warning: one should be careful about such enum/switching code. In most situations, a "real OO based" design that works with an abstract base class Command and specific subclasses is the better choice.
The real lesson here: you want to study some basics, like the tutorials found here. You see, there is no point in programming for Android ... if you don't know about such basic things such as Maps. In that sense it is hard to give you "good" advise, as the "good" stuff is that abstract base class solution - which seems to be completely beyond your current skills.
Replace your if statement with
if(str_cmd_arr[i]).equals("cmd1"){
You can use a loop and a switch statement
for example
for (int i=0;i<str_cmd_arr.length;i++){
switch(str_cmd_arr[i]) {
case "cmd1":
Log.d("TAG","perform command 1");
break;
case "cmd2":
Log.d("TAG","perform command 2");
break;
}
}

Java code is not working properly what i can do?

class java
{
public static void main(String args[])
{
System.out.println("Testing");
}
}`
not working above code and explain the above method.
`
I guess it's cause of Apostrophe on the last line of your code.
Plus a class name should always start with a capital letter, jus a bit of information thought it has nothing to do with error.
Your code is trying to print out a line on the console which is Testing
Remove the apostrophe at the last line

Can I automatically pass an existing text into the method parameter?

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);

Using || in while loop

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.

Categories

Resources