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 Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-if-else-statement-with-examples
Java if-else Statement - GeeksforGeeks
Explanation: The program declares ... if-else statement. Based on the condition, it prints either "The number is greater than 5." or "The number is 5 or less." ... If the condition yields true, go to Step 4. If the condition yields false, go ...
Published   January 19, 2026
🌐
Tutorialspoint
tutorialspoint.com › java › if_else_statement_in_java.htm
Java if-else Statement
The if statement in Java checks a Boolean expression and executes a specific block of code only if the condition is true. The if-else statement allows Java programs to handle both true and false conditions.
🌐
W3Schools
w3schools.com › java › java_conditions.asp
Java If ... Else
The condition inside the if statement must result in a boolean value - it can be either a boolean expression (like x > y) or a boolean variable (like isLightOn). Also note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error. In the example below, we test two values to find out if 20 is greater than 18.
🌐
DataCamp
datacamp.com › doc › java › java-if-else
Java If...Else
Complex conditions can be broken down into multiple if...else statements. Avoid Deep Nesting: Excessive nesting can make code hard to read. Consider using logical operators (&&, ||) or breaking logic into separate methods. Use else if for Multiple Conditions: When you have multiple conditions to check, use else if to avoid unnecessary evaluations and improve performance.
🌐
LearnYard
read.learnyard.com › java-fundamentals › if-else-statement-in-java
If-Else Statement in Java
November 8, 2024 - The curly braces indicate the body of an if statement. if the condition is true, both statements inside the curly braces will be executed and if false, both will be skipped. Let's try to understand it with the help of an example where we check whether a student is passed or failed in the examination. ... import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); // get student score from the user System.out.println("Enter score"); int studentScore = input.nextInt(); // if studentGrade is 40 or above, it means studentScore >= 40 results to true if (studentScore >= 40) { System.out.println("Yayyyy!"); System.out.println("Congrats, You passed the examination."); } } }
🌐
Programiz
programiz.com › java-programming › if-else-statement
Java if...else (With Examples)
Statement outside if...else block · Here, the value of number is -5. So the test expression evaluates to false. Hence code inside the body of else is executed. In Java, we have an if...else...if ladder, that can be used to execute one block of code among multiple other blocks.
🌐
Medium
medium.com › @AlexanderObregon › using-if-else-statements-to-handle-choices-for-beginners-in-java-d18bda75d377
Using If Else Statements to Handle Choices for Beginners in Java
June 23, 2025 - You don’t always need an else if you're only acting on some values and leaving the rest alone. This code quietly skips over totals under 100 and leaves them untouched. That gives more control without adding extra statements. Login logic is a simple example that still relies on condition checks.
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2017 › 08 › if-else-statement-in-java
If, If..else Statement in Java with Examples
September 11, 2022 - For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”. In this case we have two print statements in the program, but only one print statement executes at a time based on the input value.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › if.html
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
The if-then statement is the most ... only if a particular test evaluates to true. For example, the Bicycle class could allow the brakes to decrease the bicycle's speed only if the bicycle is already in motion....
🌐
CodeGym
codegym.cc › java blog › statements in java › if else java statements
IF ELSE Java Statements
A quick in-depth look at the various types of if statements in Java. Includes examples of if, if-else, else-if, and nested if code
Published   January 16, 2025
🌐
W3Schools
w3schools.com › java › java_conditions_elseif.asp
Java The else if Statement
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Use the else if statement to specify a new condition to test if the first condition is false.
🌐
Simplilearn
simplilearn.com › home › resources › software development › if else in java [syntax, parameters, examples]
If else in Java [Syntax, Parameters, Examples]
November 18, 2025 - If-else in Java is one of the most important decision making statements. Read the tutorial to know all about it's syntax, parameter values, and more now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Top answer
1 of 3
2

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.

2 of 3
1

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.");
                }
            }
        }
    }
}
🌐
Udemy
blog.udemy.com › home › java if else statement: mastering the flow of execution
Java If Else Statement: Mastering the Flow of Execution - Udemy Blog
October 21, 2013 - The basic structure of the If statement in Java looks like this: ... Within the brackets, you can add an action that you would like your program to perform if the conditions of the If statement are met. For instance, if you want to check whether a user is over the age of 18, you could create an If statement that looks like this: ... The above statement simply checks to see if the user is under the age of 18. At this point, the statement does nothing else and execution would continue because there is no executable statement within the brackets.
🌐
Baeldung
baeldung.com › home › java › core java › if-else statement in java
If-Else Statement in Java | Baeldung
January 8, 2024 - We can use this to choose between three or more options: if (count > 2) { System.out.println("Count is higher than 2"); } else if (count <= 0) { System.out.println("Count is less or equal than zero"); } else { System.out.println("Count is either equal to one, or two"); } ...
🌐
Vaia
vaia.com › java if else statements
Java If Else Statements: Syntax & Examples | Vaia
It can also help avoid unnecessary ... website? If Else statements can allow for conditional discounts. For example, if it's a customer's first purchase, a 10% discount can be applied....