So I'm making a program on a pretty low level of Java-programming.
This is what I'm having problems with:
//The String fillText is given a value earlier in the program
if ("".equals(txa1.getText()))
{
txa1.setText(fillText);
txa1.setVisible(true);
}
else if ("".equals(txa2.getText()))
{
txa2.setText(fillText);
txa2.setVisible(true);
}
else if ("".equals(txa3.getText()))
{
txa3.setText(fillText);
txa3.setVisible(true);
}
else if ("".equals(txa4.getText()))
{
txa4.setText(fillText);
txa4.setVisible(true);
}
else if ("".equals(txa5.getText()))
{
txa5.setText(fillText);
txa5.setVisible(true);
}
...
This code appears to ALWAYS fill all of the textareas (txaX) with fillText.
I was expecting it to only execute the first of the statements that returned true and then break out of the if-else-statement.
I tried to do it with a switch-case, but ended up failing since the String is changed during the run of the program.
What is wrong?
Thanks in advance!
It is in loop .Definetely that is causing the problem.With out loop it will go to only one block.It is not possible too execute without loop.When ever we are using if else only one block will execute.
"".equals(txa1.getText())
I think above condition for each returns true.
getText() method is always returning empty string i.e "";
You have to carefully examine your conditions, it'll basically execute the predecessors if the condition is false.
I suggest you think more on the logic of what you are trying to achieve..
Related
I'm trying to make changes to my Session servlet and they won't take effect. Basically I've written this:
isUserConfirmed = false;
if(isUserConfirmed) { do code }
else { do code }
For some reason isUserConfirmed is coming back true although I've give it a static value of false.
I'm trying to debug something and I've run into this rather annoying problem.
Help will be much appreciated.
I have a problem with an Expectations block I have written in my test case:
new Expectations() {
{
mFindHandlerMock.findAll((Model) any, (Set<Id>) any, false);
if (!pWithRealData) {
result = Collections.emptySet();
} else {
result = pAllData;
}
times = 1;
Deencapsulation.invoke(mDb, "readSqlQuery", withAny(String.class));
result = "select * from realdata";
times = 1;
}
};
the test case crashes with:
java.lang.IllegalArgumentException: Invalid conditional statement inside expectation block
exactly here:
if (!pWithRealData) {
it's just a simple boolean that is false in this case.
I have absolutly no clue why the exception happens.
I already searched with google but found nothing helpful.
Could you help me?
From the JMockit release notes for version 1.14:
Enhancement: Conditionals and loops will now trigger an exception when found inside an expectation recording block, to prevent API misuse and to encourage simpler tests. See issue #97.
The GitHub issues related to this:
https://github.com/jmockit/jmockit1/issues/97
https://github.com/jmockit/jmockit1/issues/123
In the one issue they state that:
Yes, and this is as intended, to avoid tests getting too complicated when recording expectations. A full test was not shown, but it looks to me that recording the specific expectations directly would be better in this case.
In the JMockit source you can see which other types of conditionals and loops will throw that exception.
In short, from JMockit 1.14 onwards you are not allowed to have conditionals (such as if statements) and loops in the Expectation block.
I have only ever seen something like this once before when I accidentally tried to debug in release mode, but that isn't the case here. To my question. I have this block of code in an android app:
if (leftRight[0] != null && leftRight[1] != null) {
Log.d("", "test");
return leftRight;
} else {
Log.e("", "test134");
return null;
}
When debugging, the if statement is true and it executes the first Log.d(). However, when stepping to the next line it jumps straight to the return null which I don't understand how that is possible since it is in the else rather than the if. The second Log.d() is skipped over also.
Hopefully I am just missing some small thing, but I am completely baffled as to what is going on. Anything to point me in the right direction is appreciated.
EDIT
If it matters, leftRight is a Point[] containing two Points.
Basically, you are right, it should not happen. However, consider this code:
Log.d("", "test");
if (leftRight[0] != null && leftRight[1] != null) {
return leftRight;
} else {
return null;
}
It has the same effect. Maybe your compiler restructures the code in this way and the debugger is just reflecting this as best as he can ...
EDIT: you can find out whether this is the case using "javap"
EDIT2: you may also try out whether the behavior changes if you change the log messages to do something different ...
If your function returns correct value, there is big chance there are some issues with debugger. Mismatch between lines might happened in example when your code was modified after you build it. I suggest you to clean your workspace, rebuild it and try again.
How to return to the main method and execute again the whole program? I tried the return but it just ends the execution.
This is the code:
File folder = new File("C:\my path");
File[] listOfFiles = folder.listFiles();
if (folder.isDirectory()){
if(folder.list().length>0){
System.out.println("Directory is not empty!");
}
else {
System.out.println("Directory is empty!");
return; //I need to return from the main method
}
}
else{
System.out.println("Invalid directory!");
}
I want to return to the main method if the directory is empty. I used the return but, it did not return from the start :( Any idea on how to return to the main method? Thanks :)
This is the entire main method: (Sorry if it is too long)
My goal is to check if a new file entered the directory. Then it will list the files on that directory and one-by-one it will be deleted. If there's no file from the directory, it will return to the start.
Comment correction: "//It will return to the start"
The two loops: The second loop will be executed during the execution of the first loop. (Sorry if I did not indent it). After the execution of the second loop, it will return to the first loop.
It's hard to read but I assume you want to exit the loop using return statement which is not working that way. If you want to exit a loop you have to use break.
Here's a little bit of explanation: Difference between Return and Break statements
And since it's a basic programming knowledge I advice you to go through some Java tutorial first. For example: http://www.tutorialspoint.com/java/inde
The traditional way to return all the way to the top main method, is to throw a RuntimeException (or one of its subclasses) which is caught by the main method only. Any exception handlers on the way up, must rethrow this exception after doing what they need to.
Note that your application must be written very carefully to avoid leaking resources like opened files.
Don't do everything in main method. You must extract part of your code in other declared method like:
public static void doPartOfSomething() {
...
}
public static String doOtherPartOfSomething( String path ) {
...
return "aresult"; // Stop the execution of this method
}
Please note that the second one takes a parameter and returns a result when is called (a String here)
And then, from main method (or other place), you can call this piece of code several times:
doPartOfSomething();
doPartOfSomething();
String result = doOtherPartOfSomething( "/test" );
...
Doing that, you can execute some piece of code, return and stop a part of code, and continue with another call into the calling method.
I think you should learn more about classes, objects, methods and fields in java.
I have a strange problem when I'm trying execute a cypher query in an java application.
The result.dumpToString()- method shows me the correct result.
But when I'm trying to iterate, the last node is always missing (for every executed query):
for (Map<String, Object> row : result) {
System.out.println(((Node) row.get("A")));
System.out.println(((Node) row.get("A")).getProperty("name").toString());
}
The first output is correct. I see all nodes of the result.
In the second output one node is missing, although I know, that the node has a "name"-property.
Has someone an idea?
Thank you
If you're missing a second output, it's likely that the value of that property is a string that is blank. This line:
System.out.println(((Node) row.get("A")).getProperty("name").toString());
In the presence of an attribute "name" that is blank, this will print nothing at all (but a linefeed).
Also, the way you're doing this is a bit dangerous; keep in mind that in general nodes can be missing, so getProperty("name") can return null. Meaning that when you call toString() on it, you can end up with a NullPointerException. It's better to maybe write either this:
row.get("A").getProperty("name", "missing").toString();
That will return "missing" if the property is missing, or:
Object propValue = row.get("A").getProperty("name");
if(propValue != null)
System.out.println(propValue.toString());
else System.out.println("Missing name property");
Solved my problem:
I was executing the query without beginning a Transaction.
Now it works. Nevertheless this is a strange behaviour.
Thank you all