Use the ternary operator:
name = ((city.getName() == null) ? "N/A" : city.getName());
I think you have the conditions backwards - if it's null, you want the value to be "N/A".
What if city is null? Your code *hits the bed in that case. I'd add another check:
name = ((city == null) || (city.getName() == null) ? "N/A" : city.getName());
Answer from duffymo on Stack OverflowVideos
Use the ternary operator:
name = ((city.getName() == null) ? "N/A" : city.getName());
I think you have the conditions backwards - if it's null, you want the value to be "N/A".
What if city is null? Your code *hits the bed in that case. I'd add another check:
name = ((city == null) || (city.getName() == null) ? "N/A" : city.getName());
To avoid calling .getName() twice I would use
name = city.getName();
if (name == null) name = "N/A";
You're missing some }s after your else clauses. Example:
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
//Put code that should only execute if you select Compact here.
}else if(response.equals("F")){
System.out.println("You have selected Full-Size. ");
//Put code that should only execute if you select Full-size here.
//Should have a } here!
//Put code that should always execute here.
Because you never close the block of code in the else clause, all of the code that follows is still part of the else, and therefore will only be executed if the else is selected, not under every circumstance as you had intended.
You are opening lots of brackets { but not closing them where you need }.
I usually not just handing the code, but I've noticed you done must of the job, but confused where to close the brackets and a little bit at the program flow.
I only changed it a bit, there is a lot that you can cut and reuse code.
public static void main(String[] args){
for(int i = 0; i < 4; i++) {
System.out.println("Programmed by .");
double standardCompact = 30.50;
double couponCompact = ((30.50)-(30.50 * 0.07));
double standardFullSize = 40.50;
double couponFullSize = ((40.50)-(40.50 * 0.07));
//Scanner Input
Scanner input = new Scanner(System.in);
System.out.print("Rent a Car? [Y or N]: ");
//Response String
String response = input.next().toUpperCase();
if (response.equals("N")){
System.out.println("You entered no. Bye. ");
}
else if (response.equals("Y")) {
System.out.print("Compact or Full-Size? [C or F]: ");
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
//case1
System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardCompact + " " + "per day.");
}
else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponCompact + " " + "per day.");
}
}
else if(response.equals("F")) {
System.out.println("You have selected Full-Size. ");
//case 2
System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardFullSize + " " + "per day.");
}
else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponFullSize + " " + "per day.");
}
}
}
}
}
If you want to convert something like:
if(A) {
return X;
}
else if(B) {
return Y;
}
else {
return Z;
}
You can write this as:
A ? X : (B ? Y : Z);
You thus write the else if as a condition in the else-part (after :) of the upper expression.
However, I would strongly advice against too much cascading. The code becomes extremely unreadable and the ? : code structure was never designed for this.
You can extend this to any number of clauses, in perfect analogy to the if-else construct.
return a == b? "b"
: a == c? "c"
: a == d? "d"
: "x";
In this form it quite closely resembles Lisp's cond, both in shape and in semantics.
But, do note that this is not a "shorthand for if/else" because it is an expression whereas if/else is a statement. It would be quite bad abuse of the ternary operator if the expressions had any side effects.
else if(month = 2 && day > 28)
this.day = 0;
(I know this code is incorrect, it had an error to it when I was typing it). What do you think would be the best way for me to go about making sure the day is not greater than 31, 30, or 28 depending on the month?
The error is NOT in the else if part, but in the month = 2 part.
You are actually assigning 2 to month instead of comparing it.
use month == 2 and your code will just work fine.
also I am not sure if 'else if' is supported in Java?
It is very much supported in Java as well.
if(day < 1 || day > 31)
this.day = 0;
else
this.day = day;
}
If condition for day < 1 || day >31 cover's up everything, no need for else if, but you might need to consider for 30 days month as per your Business logic
else if(month = 2 && day > 28)
this.day = 0;
= is for assignment, and == is for comparison, Need to compare using ==,
else if(month == 2 && day > 28)
this.day = 0;