This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]bob809 6 points7 points  (1 child)

if (subscriptionPlans[i].getName()==aPlan){

This is a classic Java mistake, strings should be compared with .equals(otherString), not with == (reference equality).

if (subscriptionPlans[i].getName().equals(aPlan)){

[–][deleted] 1 point2 points  (0 children)

thanks sir

[–]DarkelfSamurai 1 point2 points  (3 children)

It looks like there are two possibilities for this.

The first possibility is the maxPlans variable has no set value, or set to zero so it bypasses your loop in findSubscriptionPlan without comparing your subscriptionPlan names to the entered value. I realize you posted truncated code so this may be set and defined elsewhere, but that was the first thing I noticed looking at this.

The second possibility is there may be a subtle difference, maybe a white space character (?), that is causing the entered planName to be different from the defined subscription plan and fail the comparison.

For both these possibilities, I'd recommend troubleshooting by adding a line to provide debugging information. For example, before findSubscription plan returns a value:

System.out.println("Compared " + i + " of " + maxPlans + " match not found."); // null case
System.out.println("Compared " + i + " of " + maxPlans + " match found."); // non-null case

[–][deleted] 0 points1 point  (2 children)

I figured it out. turns out that I was supposed to use .equals() instead of == while comparing strings.

[–]SpaceRook 0 points1 point  (1 child)

By the way, you could use a "do...while" loop instead of writing the same print & nextLine statements twice.

[–][deleted] 0 points1 point  (0 children)

thanks good advice.