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
🌐
W3Schools
w3schools.com › java › java_conditions.asp
Java Conditions and If Statements
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-if-statement-with-examples
Java if statement - GeeksforGeeks
October 14, 2025 - Only the first statement after if is executed when the condition is true. The second System.out.println() is not part of the if block, so it executes in all cases.
🌐
Jenkov
jenkov.com › tutorials › java › if.html
Java if statements
April 13, 2024 - The if statement in this example tests the boolean variable isValid and based on its value (either true or false) it executes one of two different blocks of code. If the isValid variable has the value of true, the first block is executed.
🌐
Tutorialspoint
tutorialspoint.com › java › if_statement_in_java.htm
Java - if Statement
If not, the first set of code after the end of the if statement (after the closing curly brace) will be executed. In this example, we're showing the use of a if statement to check if a value of variable, x is less than 20.
🌐
DataCamp
datacamp.com › doc › java › if
if Keyword in Java: Usage & Examples
In this example, the if condition number > 10 is false, so it checks the else if condition number > 5, which is true. Therefore, the message "The number is greater than 5 but less than or equal to 10." is printed. Readable Conditions: Ensure that the conditions within if statements are easy to understand and maintain.
🌐
Programiz
programiz.com › java-programming › if-else-statement
Java if...else Statement
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.
🌐
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....
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2017 › 08 › if-else-statement-in-java
If, If..else Statement in Java with Examples
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.
🌐
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
🌐
YouTube
youtube.com › watch
Java if statements 🚧【6 minutes】 - YouTube
java if statements#Java #if #statements
Published   October 19, 2020
Top answer
1 of 3
1

If statements are sometimes hard to understand if they aren't explained well. In hopes to help I'll show you some examples and explain how they work.

First you need to understand that an if statement is not as simple as True or False. It works more as if the given situation in code meets the requirements you set for it between the parenthesis it will run the code inside the brackets of the if statement.

Example 1:

int x = 5;

if(x == 5)
{

     System.out.println(" X is equal to 5");

}

Whats happening hear is you are initializing the int x to be 5 and then the if statements runs to see if x is equal to 5. Since it is the code inside the if statement is run and it prints out the sentence. X is equal to 5.

If the the int x was equal to anything but five in this situation the if statement would not run the code inside because it failed to meet the requirements. Similarly you can use different operators such as; does not equal(!=), greater than or less than(<, >), and greater to or equal to and less than or equal to(<=, >=).

If you want to get more complex you can go as far as to add and or or operators in(||, or &&). This allows you to set multiple requirements in one if statement.

Example 2:

int x = 5;
int y = 2;

if(x == 5 || y == 5)
{

     System.out.println("I <3 if statements");

}

What is happening in this example is the if statement is checking to see if either y or x is equal to 5. Since one of them is (x) it runs the code inside the if statement printing out the sentence. I <3 if statements.

With the use of the or operate only if neither of the requirements are met will it not operate the code inside. Having both the requirements be met is fine because at least one of them are.

Also when using || you are not limited to only 2 requirements you can go on to make as many as you desire.

Example 3:

int x = 5;
int y = 2;

if(x == 5 && y == 2)
{

    System.out.println("Coding is fun");

}

With the and operator, the if statement checks to see if both the requirements are met. In this example since x is equal to 5 and y is equal to 2 the code in the if statement will run printing the text. Coding is fun.

If you look to get more in depth you can also use else if and else statements. How the work is simply if the requirements of the previous if statements were not met than it will either run the code if it is an else statement or check to see if the next set of requirements are met in the else if statement.

Example 4:

int x = 5;

if(x == 1)
{

    System.out.println("X is 1");

}

else if(x == 3)
{

    System.out.println("X is 3");

}

else
{

    System.out.println("X is unknown");

}

What is happening is that the original if statement is checking to see if x is equal to 1. Since it is not the code inside the if statement is not run and it moves on to the else if statement. The else if statement is checking to see if x is equal to 3. Once again since it is not it skips over the code inside the else if statement and moves the the else statement. With the else statement since there is no requirements it runs the code inside no matter what and finally prints out the sentence. X is unknown.

In the event that the requirements in one of the previous statements(if, or else if) is met it will run the code inside the given one and terminate there. In other words it won't run the else regardless, only if everything else fails.

I hope I was able to help with your problem. Enjoy your coding experiences! :)

2 of 3
0

I'm not sure what you mean by "thorough research on If statements", but the principle is simple.

There's always some kind of expression that might turn out to be true or false. It's written in parentheses. Then there's a bunch of statements that are usually (but not always) written between curly braces. Some naughty developers sometimes omit the braces, if there's only one statement, but it's arguably best not to do this.

The computer starts by working out whether the expression in parentheses is true or false. If it's true, the statements in the braces are run. If it's false, the statements in the braces are ignored.

For example,

if (today.equals("Friday")) {
   developers.goHome("early");
   managers.stay("late");
}

Here, if today.equals("Friday") turns out to be true, the two statements inside the braces are run. Otherwise, they are not.

🌐
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
🌐
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
🌐
Tutorialspoint
tutorialspoint.com › java › if_else_statement_in_java.htm
Java if-else Statement
Nested if-else Allowed: You can place an if or else if statement inside another if statement (nested if-else). Multiple else-if Conditions: The else if condition can be used multiple times before the final else statement. Execution Order: Java executes the if-else conditions from top to bottom, and once a condition is met, the remaining conditions are ignored.
🌐
Javatpoint
javatpoint.com › java-if-else
Java if-else Statement
Java if else statement, if else statement in java, java if statement, java multiple if, java if-else, java if-else-if, java if else if ladder statement, and java nested if with concepts and examples, java control statements.
🌐
GeeksforGeeks
geeksforgeeks.org › java › decision-making-javaif-else-switch-break-continue-jump
Decision Making in Java - Conditional Statements - GeeksforGeeks
The below diagram demonstrates the flow chart of an "nested-if Statement execution flow" in programming. ... The if-else-if ladder allows multiple independent conditions to be checked in order. As soon as one condition is true, its block executes, and the rest are skipped. ... import java.util.*; class Geeks { public static void main(String args[]) { int i = 20; if (i == 10) System.out.println("i is 10"); else if (i == 15) System.out.println("i is 15"); else if (i == 20) System.out.println("i is 20"); else System.out.println("i is not present"); } }
Published   October 6, 2025
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java if statement tutorial with examples
Java If Statement Tutorial With Examples
April 1, 2025 - Initially, we have taken a number through the console using the Scanner class. Then, we have checked the condition for the positive and negative scenarios using the if-else statement. Finally, we have added a default condition where we have mentioned that the number must be zero if it does not match the above-specified conditions. import java.util.Scanner; public class example { public static void main(String[] args) { System.out.println("Enter the number: "); // Taking input from the console int num; Scanner in = new Scanner(System.in); num = in.nextInt(); // conditional check for age criteria if(num < 0){ System.out.println("Negative number"); } else if(num > 0){ System.out.println("Positive number"); } else{ System.out.println("Number is zero"); } } }
🌐
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.
🌐
ScholarHat
scholarhat.com › home
Control Statements in Java with Examples: If, If-Else & Switch
September 6, 2025 - This article gives a vast idea of conditional statements and their types. Features of every conditional operator in Java with examples and syntax are explained. This article also includes the different types of conditional statements in Java and the difference between the If-else condition and the switch condition.