Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
public String auxToPostfixString(Node root) {
String result = "";
if (root == null) {
return "";
}
result += auxToPostfixString(root.getLeft());
result += auxToPostfixString(root.getRight());
result += root.getExp();
return result;
}
I used this code for that, and it should return 342*+8+ but it returns 34+2*8+ (the original expr is 3+4*2+8) What's wrong about this?
Sorry for bad English
it should return 342*+8+ but it returns 34+2*8+ (the original expr is 3+4*2+8)
the problem may come from the original parsed tree : was the priority of multiplication against addition well applied ? Make sure that the tree is not, in fact, equivalent to ((3+4)*2+8).
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
for (Pair p : pairs) {
double f = foo(p)
...
}
foo() performs a simple mathematical calculation as follows:
double foo(Pair p) {
return Math.cos(p.x) + Math.sin(p.y);
}
If all the items in pairs are the same, can the Java compiler optimise this using Common subexpression elimination? Or is there another optimisation that occurs? I am asking since I have found a significant time reduction when more values in pairs are the same.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I don't have much experience with Java, but I use ternary operators in C#, and it's such a trivial thing that I don't think about it when I use it.
Recently I started a Spring Boot project, and I have something like this:
dto.setProfilePhoto(dao.getProfilePhoto() != null ? dao.getProfilePhoto() : "images/default-img.png");
but the profile photo always came back null.
Then I turned on debugger mode, and evaluated some expressions and this totally confused me...
Expression: boolean t = true ? true : false; ---> null
Expression: boolean t = 10 > 2 ? true : false; ---> null
Why do I get null values for these expressions?
Edit: Here are the getter and setter
public String getProfilePhoto() {
return ProfilePhoto;
}
public void setProfilePhoto(String profilePhoto) {
ProfilePhoto = profilePhoto;
}
You have not mentioned the implementation of dao.getProfilePhoto() e.g. it might be advancing to null in the second call. You can try it as follows:
String profilePhoto = dao.getProfilePhoto();
dto.setProfilePhoto(profilePhoto != null ? profilePhoto : "images/default-img.png");
It is not only efficient but also makes it agnostic of the implementation of dao.getProfilePhoto().
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am working on a text editor software(kinda like notepad) and whenever I open files from Documents, correct data is displayed but at any other location Null is returned
This is because bff.readLine() is returning null. According to the documentation, it returns null if the end of the stream has been reached.
The previous check bff.readLine() != null doesn't help, because each call advances the reader. Try it like that:
String line;
while ((line = bff.readLine()) != null) {
sk += line + "\n";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have been working on a data compression and decompression program in java. At some point in my code, I want to visit only the nodes with keys. The part of the code looks like this:
//visit only nodes with keys
if(n.alpha != '\0') {
System.out.println("{" + n.alpha + ":" + s + "}");
charToCode.put(n.alpha, s);
codeToChar.put(s, n.alpha);
}
'\0' gives me an invalid character constant. I need to know what is going on, and how I can remedy the situation. Thanks!
You're trying to represent and treat a String as a char
n.alpha != '\0' // single quotes denote a character
You need to use a String instead
n.alpha != "\0"
Then, because the equality operator generally shouldn't be used with Strings, and because you're incorrectly testing the equality between a String and a char, you need to rewrite it as
if(!Character.toString(n.alpha).equals("\0"))
Note the use of Character.toString(n.alpha) to convert the char n.alpha to a String.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I spent a long time on this. Why didn't it give some sort of error? I don't understand:
This java code snipette (I will put comments for code I omitted just for the example):
String[] sortOrder = { "Ascending","Descending" }
for ( String order : sortOrder ) {
if (sortOrder.equals("Ascending")) {
// code here to sort array in ascending
} else if ( sortOrder.equals( "Descending") ) {
// code here to sort descending
}
}
In the javadocs it states:
true if this object is the same as the obj argument; false otherwise.
The equals method is returning false instead of an error because your array is not equal to the
string value of "Ascending" and "Descending"