Comparing Strings Java
I am trying to compare a string to a string value. Seems rather simple, however, the comparison is returning null. All I want is to output the matched value and ignore the null return. But the output is showing the null value as well. I have tried this in various ways, but it keeps showing the null value.
class ActionMovie extends cdinve开发者_运维百科ntoryprogram {
private String Atitle;
private double Avalue;
private double Rstock;
private String Ctitle;
public ActionMovie(String title, int itemNumber, int numberofUnits, double unitPrice ){
Atitle = title;
Avalue = numberofUnits * unitPrice;
Rstock = unitPrice * .05;}
public String getActionTitle(){
if (Atitle.equals("Matrix")){
Ctitle = Atitle;
}else if (!Atitle.equals("Matrix")){
}
return Ctitle;
}
}
public class cdinventoryprogram {
public static void main(String[] args) {
ActionMovie myAction[] = new ActionMovie[DEFAULT_LENGTH];
myAction[0] = new ActionMovie ("The Illusionist", 1, 5, 15.99);
myAction[1] = new ActionMovie ("Matrix", 2, 3, 14.99);
myAction[2] = new ActionMovie ("Old School", 3, 6, 12.99);
for ( ActionMovie currentActionMovie : myAction ){
CAction = currentActionMovie.getActionTitle();
JOptionPane.showMessageDialog( null, "Your action movie is: " + CAction);
}} }
So you want to return an empty string instead of null? Then just do that so.
if (Atitle.equals("Matrix")){
Ctitle = Atitle;
} else {
Ctitle = "";
}
return Ctitle;
Note that the second if
is pretty redundant, so I removed it as well.
Be careful with potential NullPointerException
whenever Atitle
is actually null
. To prevent from that, you'd like to do it as follows since "Matrix"
is never null
.
if ("Matrix".equals(Atitle)){
Ctitle = Atitle;
} else {
Ctitle = "";
}
return Ctitle;
Unrelated to the problem, I'd suggest to get yourself through the standard Java naming conventions. Class names ought to start with uppercase and variable names with lowercase. This way the code is better readable for every other Java developer (including yourself in the future).
Update as per the comment:
No, I don't want to return anything if the strings don't match. It is returning a value if not equal. – user569127 3 mins ago
Then just return null
. This only shifts the problem to displaying the value. If it is null
then just don't display it. E.g.
if (CAction != null) {
JOptionPane.showMessageDialog(null, "Your action movie is: " + CAction);
}
The confusion in this question is likely caused by your confusion of the terms "returning" and "displaying".
Change:
private String Ctitle;
To:
private String Ctitle = ""
If Atitle is not equal to Matrix, Ctitle will always be null. Set a default value for Ctitle so it never returns null.
The problem is that your member variable CTitle
is initialized (by default) to null
and your getActionTitle()
method only updates its value if it equals the string "Matrix" (otherwise there is no change to its value). So that method will return its default value (null) which prints as the string "null".
If you don't want to see "null" when you are printing the return value somewhere then you should check for null and act accordingly, or change the default value of CTitle
to, say, the empty string (""
).
Are you certain that Atitle is getting set properly and that it at one point is equal to "Matrix"? I would suggest setting a break point in your code to check the value of Atitle before it hits the comparison. You might also initialize Ctitle in your constructor.
精彩评论